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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12823976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:36:47  来源:igfitidea点击:

How to modify git add to handle deleted files?

git

提问by James Raitsev

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

我从我的 git repo 中删除了一些文件,现在,在状态看到

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

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 add .命令来暂存它们,但这样做不会影响 git 状态。文件仍未暂存以进行提交。

Now .. i know i can git rm fileto take care of this.

现在..我知道我可以git rm file照顾这个。

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 .以某种方式修改也可以暂存已删除的文件吗?我想加“。” 处理一切(包括已删除的文件)

回答by cdhowie

git add .will add new and modified files to the index. git add -uwill 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将在磁盘上删除文件并更新修改后的文件时从索引中删除文件,但不会添加新文件。你需要两者的结合:

git add . && git add -u .

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

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

git add -A .

Note the extra '.' on git add -Aand git add -u

注意额外的“.” 在git add -Agit add -u



Warning, starting git 2.0 (mid 2013), git add -A|u(not extra dot) will alwaysstage 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 2.0(2013 年中)开始,git add -A|u(不是额外的点)将始终在所有工作树上暂存文件。
如果您只想使用该工作树在当前路径下暂存文件,则需要使用

$ git add -A .

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

参见“ git add -A”和“ git add .”的区别