"git nah" and more handy Git aliases

Published 12/1/2019

This article is part of a series:


A while ago I posted an article on undoing changes in Git, but some of these arguments or flags just don't want to stick in my head or, I use them so often I don't want to write them out every time. Luckily Git provides us with the ability to create our own custom commands.

For example, if you want to just do git c instead of git commit you could register the following alias:

git config --global alias.c "commit"

With that being said, I want to introduce my top three git aliases:

git nah

Did you ever write something out and then felt like "nah...". Now you can put your thoughts into action with this handy alias. Reset any local and staged changes as if nothing happened.

git config --global alias.nah "!git reset --hard && git clean -df"

Notice the exclamation mark. It needs this so we can run custom commands. This allows us to combine two git commands.

Check out my e-book!

Learn to simplify day-to-day code and the balance between over- and under-engineering.

git amend

Sometimes you just miss committing something. Adding more changes to the latest commit was never easier. It doesn't even open vim for you to change the commit message, it just appends to the latest commit.

git config --global alias.amend "commit --amend --no-edit"

git update

While working on a feature branch, more than often, by the time you want to merge back into the main branch (develop/master), that main branch already has a few new commits. Keeping your local branch up to date will help avoiding big merge conflicts. I use this one daily.

git config --global alias.update "pull --rebase origin develop"

bonus

Adding "-n" when committing allows you to skip precommit hooks like linting. This is sometimes necessary when working with legacy code. The following alias at least makes you feel bad about it.

git config --global alias.commit-crime "commit -n"

Last but not least here is a handy alias to list all aliases. Let's name it "alias".

git config --global alias.alias "! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /"