How to 'git rm' all deleted files shown by 'git status'
UPDATE: Thanks to user comments, it turns out the easiest way to do this is with this command:
git add -u
Otherwise, feel free to read the example below for an alternative approach...
===============================
So today I was updating the filefield module in Drupal 6 with Drush (drush up webform), this worked great, as usual. However, my Drupal site is under source control with GIT. So after updating the module and running 'git status' I was presented with a whole bunch of changes. Something like this:
git status
# Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: filefield/README.txt # modified: filefield/UPGRADE.txt # ... many more files listed ... # deleted: filefield/filefield_meta/translations/de.po # ... many more files listed ... # deleted: filefield/translations/uk-ua.po # deleted: filefield/translations/uk.po
There were about 30 files that were deleted, so we definitely don't want to have to execute 'git rm' 30 times, once for each deleted file. Let's use some terminal magic to take out the garbage in one foul swoop, no pun intended. First, let's get our list of files to verify we are about to 'git rm' the correct files:
git status | grep deleted | awk '{print $2}'
The trick with 'awk' is that the $3 says "print the third column in the line of text". It determines columns via its default column separator, which is whitespace. For example:
# deleted: filefield/translations/uk.po
So in this example, the first column is '#', the second column is 'deleted:' and the third column will be the file name 'filefield/translations/uk.po'.
Now that we have our complete list of deleted files, we can 'git rm' them:
git rm `git status | grep deleted | awk '{print $2}'`
The command essentially says, "Use grep to find all the lines ouput by 'git status' that have the word 'deleted' in it, then grab the 3rd column on the line and run 'git rm' on it."
That will do it!
Comments
Jonas Fagundes (not verified)
Thu, 11/08/2012 - 06:30
Permalink
$ git add -u .
$ git add -u .
does the trick :)
Cheers,
tyler
Thu, 11/08/2012 - 10:43
Permalink
Sweet nectar, I will have to
Sweet nectar, I will have to give this a try next time I run into this dilemna, thank you!
JP (not verified)
Tue, 08/06/2013 - 07:50
Permalink
confirmed, git add -u is the
confirmed, git add -u is the best way to do it.
Thanks Tyler, however, for the informative trick. I learned something new about awk and about piping commands. Really cool.
I have a question you can probably answer. Rather than using the `git status...` command as input for git rm, how would one go about feeding it a text file?
Like let's say I need to further manipulate the list of files, for example lines corresponding to files I want to keep.
I did this:
git status | grep deleted | awk '{print $3}' > files_to_be_removed.txt
Then, after editing the text file, how do I use that as input for the rm command? This doesn't work:
git rm < files_to_be_removed_edited.txt
Any ideas?
Thanks, -JP
tyler
Tue, 08/06/2013 - 09:40
Permalink
I would try something like
I would try something like this:
rob (not verified)
Thu, 03/07/2013 - 10:17
Permalink
Thanks bunch both of you,
Thanks bunch both of you, just what I needed.
Robin Adams (not verified)
Mon, 06/24/2013 - 07:15
Permalink
git rm $(git ls-files -
tyler
Fri, 06/02/2017 - 14:18
Permalink
Robin, thank you for pointing
Robin, thank you for pointing out git ls-files, and being able to wrap it with rm $(), these are very useful. I used them to remove all untracked files:
Techplex Engineer (not verified)
Tue, 07/16/2013 - 11:45
Permalink
Add this as an alias!
I figured out how to add this to my .gitconfig:
ra = "!git rm `git status | grep deleted | awk '{print $3}'`"
Where the 'ra' stands for 'remove all'
So I can type:
$ git ra
Hope this helps,
Blake
tyler
Fri, 07/19/2013 - 14:32
Permalink
Thanks Blake!
Thanks Blake!
anonymous (not verified)
Fri, 07/19/2013 - 18:34
Permalink
to avoid problems with
to avoid problems with "deleted" in the filename:
git status | awk '$2 == "deleted:" {print $3}'
Deewendra (not verified)
Thu, 08/22/2013 - 16:26
Permalink
Thanks that definitely saved
Thanks that definitely saved me some headaches. :)