Lazy One Liners: 'Git Rm' All Deleted Files

[UPDATE] My git skills are weak. You can achieve this much more simply by typing: git add -A. Still, knowing about the --porcelain option is useful. Honest.

Deleted three hundred and seventy two files and forget to use git rm? This one liner will do it for you, if you run it from your repository root:

git status --porcelain | awk '/^.D .*$/ {print $2}' | xargs git rm

The --porcelain command is great for batch manipulations of your staged and unstaged files. Just replace the D in /^.D .*$/ with the status letter for the sort of files you want to perform a batch operation on, and then replace the git rm at the end of the command with whatever you want to do to them. The filename will be passed at the end of the git command.

When you change the letter, where you place the dots are important. .D is used for unstaged deleted files, whereas D (no dot before) indicates a staged deleted file. Those 2 columns can have many character combinations, so you may have to play with the awk matching string a little to filter what you want. To test the pattern safely, you can use:

git status --porcelain | awk '/^.D .*$/'

This will print any matching lines. If there’s no output, the filter doesn’t match anything.

You can find more information about the status letters in the man page for git-status.