My Favorite Git Alias
As pretty much every developer, I am lazy and try to make my life super easy. Being lazy often involves creating alias for every thing you type more than twice. And as I use git all day long, creating git aliases is a logical path.
One of my favorite git alias is this one:
rmall = !git rm $(git ls-files -d)
Everything starts with the git rm which removes files from the working tree and the index. So to remove a file you usually type
$ git rm path/to/my/file.js
But when you have a lot of files to remove and you are lazy, it can be annoying to type the name of every file. This is where the second part gets interesting.
Yo dawg I heard you like git
The following command
$ git ls-files -d
shows you the files that have been removed (-d stands for -deleted). So if you combine the two together you can remove every files that has been deleted in one command.
To do this, wrap the second command in $(). Which gives us:
$ git rm $(git ls-files -d)
Boom. With one command you removed every files. But this command is way too long for my lazy fingers so let’s create an alias.
Creating an alias for git is really easy. I will assume you already know how to create some, but if that’s not the case you can learn here.
With this command though, there is a subtlety. Let’s try and add this to the git config file:
[alias] rmall = rm $(git ls-files -d)
If you try to run this with git rmall, git will throw an error:
error: unknown switch 'd'
All we need is to add a ! in front of the command to tell git to run this as a command to the bash:
[alias] rmall = !git rm $(git ls-files -d)
And you’re good to go.
If you’re interested in seeing others git aliases, you can check out my dotfiles.