Delete files from git index when they are already

2019-01-21 02:07发布

问题:

I have a bunch of files deleted from the fs and listed as deleted in git status.

How can I stage this changes faster then running git rm for each file?

回答1:

You can do this with:

git ls-files --deleted -z | xargs -0 git rm

Whenever this question is asked, people suggest git add -u, but the problem with that answer is that it also stages other modifications in your working copy, not just deletions. That might be OK in many situations, but if you want to just stage the deletion of files that have been deleted from the working copy, the suggestion I've made is more precise.

There's actually a section of the git rm documentation that discusses how to do what you want - I believe that the command suggested in the "Other ways" section is equivalent to what I've suggested here.



回答2:

Use the -u-flag: man git-add

git add -u .


回答3:

git commit -a would stage deleted files (as well as modified) and prompt you for your commit message. I usually execute git commit -av (verbose) to also see diffs of modified files.

From the manual page:

by using the -a switch with the commit command to automatically "add" changes from all known files (i.e. all files that are already listed in the index) and to automatically "rm" files in the index that have been removed from the working tree, and then perform the actual commit;



回答4:

git add -A

will do the job for you



回答5:

You can use git rm --cached "path/to/file" to stage a single deleted file.

Use git rm -r --cached -- "path/to/directory" to stage a complete deleted directory.