Sometimes I use
rm instead of
git rm to delete files in a git repository.
This simple mistake means that the deleted files are not staged when I try to commit my changes.
A short post on how to use xargs I found this morning led me to a simple solution:
git ls-files -d | xargs git rm
How does this work?
git ls-fileslists all the files tracked by the git repository.
- Passing the
-dflag only lists the deleted files.
xargsthen iterates over each line in the list of files while prepending its arguments.
Each deleted file as identified by
git ls-files gets converted to
git rm FILENAME and then executed.
In the past I had to copy and paste lines out of
git status or type out the filenames without the benefit of tab completion in order to stage all the deleted files.
I don’t have to do that now.
To speed things up even further I added an alias to my dotfiles.