Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files from git index when they are already deleted from fs

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?

like image 960
Bogdan Gusiev Avatar asked May 22 '11 20:05

Bogdan Gusiev


People also ask

How do I remove a deleted file from my git repository?

Delete Files using git rm. The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.

Does git add/remove deleted files?

To add a single file to the commit that you've deleted, you can do git add what/the/path/to/the/file/used/to/be . This is helpful when you have one or two deletions to add, but doesn't add a batch of deletions in one command.


2 Answers

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.

like image 56
Mark Longair Avatar answered Oct 18 '22 21:10

Mark Longair


Use the -u-flag: man git-add

git add -u . 
like image 28
KingCrunch Avatar answered Oct 18 '22 23:10

KingCrunch