Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home1/tylerfra/public_html/includes/common.inc).

How to 'git rm' all deleted files shown by 'git status'

Category: 

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

$ git add -u .

does the trick :)

Cheers,

tyler's picture

Sweet nectar, I will have to give this a try next time I run into this dilemna, thank you!

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's picture

I would try something like this:

cat files_to_be_removed_edited.txt | git rm

Thanks bunch both of you, just what I needed.

git rm $(git ls-files --deleted) will also work.
tyler's picture

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:

rm $(git ls-files --others --exclude-standard)

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's picture

Thanks Blake!

to avoid problems with "deleted" in the filename:

git status | awk '$2 == "deleted:" {print $3}'

Thanks that definitely saved me some headaches. :)