本文共 2164 字,大约阅读时间需要 7 分钟。
本文翻译自:
Say I have a file in my git repository called foo
. 假设我的git储存库中有一个名为foo
的文件。
Suppose it has been deleted with rm
(not git rm
). 假设已使用rm
(不是git rm
)将其删除。 Then git status will show: 然后git status将显示:
Changes not staged for commit: deleted: foo
How do I stage this individual file deletion? 如何分阶段删除单个文件?
If I try: 如果我尝试:
git add foo
It says: 它说:
'foo' did not match any files.
参考:
Use git rm foo
to stage the file for deletion. 使用git rm foo
要删除的文件。 (This will also delete the file from the file system, if it hadn't been previously deleted. It can, of course, be restored from git, since it was previously checked in.) (如果以前没有将其删除,这也会从文件系统中删除该文件。由于先前已将其检入,因此当然可以从git中还原它。)
To stage the file for deletion without deleting it from the file system, use git rm --cached foo
要git rm --cached foo
要删除的文件而不将其从文件系统中删除,请使用git rm --cached foo
Even though it's correct to use git rm [FILE]
, alternatively, you could do git add -u
. 即使使用git rm [FILE]
是正确的,也可以执行git add -u
。
According to the git-add
documentation: 根据git-add
文档:
-u --update -u-更新
Update the index just where it already has an entry matching . 在索引中已有条目匹配的地方更新索引。 This removes as well as modifies index entries to match the working tree, but adds no new files. 这会删除和修改索引条目以匹配工作树,但不会添加任何新文件。
If no is given when -u option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories). 如果在使用-u选项时未指定任何值,则将更新整个工作树中的所有跟踪文件(旧版本的Git用于将更新限制为当前目录及其子目录)。
Upon which the index will be refreshed and files will be properly staged. 索引将在此刷新,文件将被适当地暂存。
You can use 您可以使用
git rm -r --cached -- "path/to/directory"
to stage a deleted directory. 暂存已删除的目录。
to Add all ready deleted files 添加所有准备好的已删除文件
git status -s | grep -E '^ D' | cut -d ' ' -f3 | xargs git add --all
thank check to make sure 感谢检查以确保
git status
you should be good to go 你应该很好去
To stage all manually deleted files you can use: 要暂存所有手动删除的文件,可以使用:
git rm $(git ls-files --deleted)
To add an alias to this command as git rm-deleted
, run: 要将别名添加为git rm-deleted
,请运行:
git config --global alias.rm-deleted '!git rm $(git ls-files --deleted)'
转载地址:http://syexb.baihongyu.com/