如何修改git add来处理已删除的文件?

问题描述:

我从git存储库中删除了一些文件,现在,根据状态查看

I removed a few files from my git repo and now, upon status see

# Changes not staged for commit:
# ...
#   deleted:    project/war/favicon.ico
#   deleted:    project/war/index.html

通常,我会通过发出git add .命令来暂存它们,但这样做不会影响git的状态.文件仍未暂存以提交.

Usually, i would stage them by issuing git add . command, but doing so does not affect git status. Files are still not staged for commit.

现在..我知道我可以git rm file来解决这个问题.

Now .. i know i can git rm file to take care of this.

问题是...我可以以某种方式修改git add .以便也删除文件吗?我以为加."负责一切(包括已删除的文件)

The question is ... can i modify git add . somehow to also stage deleted files as well? I thought add "." takes care of everything (deleted files included)

git add .将向索引中添加新文件和修改过的文件. git add -u将在磁盘上删除文件时从索引中删除文件并更新已修改的文件,但不会添加新文件.您需要将两者结合起来:

git add . will add new and modified files to the index. git add -u will delete files from the index when they are deleted on-disk and update modified files, but will not add new files. You need a combination of the two:

git add . && git add -u .

附录:-A开关似乎将捕获所有三个文件:添加,修改和删除的文件.

Addendum: It appears that the -A switch will catch all three: added, modified, and deleted files.

git add -A .

请注意多余的.".在git add -Agit add -u

Note the extra '.' on git add -A and git add -u

警告,从git 2.0(2013年中)开始,git add -A|u(不是多余的点)将始终放在所有工作树上的阶段文件.
如果您只想在具有该工作树的当前路径下暂存文件,则需要使用

Warning, starting git 2.0 (mid 2013), git add -A|u (not extra dot) will always stage files on the all working tree.
If you want to stage file only under your current path with that working tree, then you need to use

$ git add -A .

请参阅""git add -A"和"git add ." "的区别.

See "Difference of "git add -A" and "git add ."".