DEV Community

Michael Z
Michael Z

Posted on • Updated on • Originally published at michaelzanggl.com

"git nah" and more handy Git aliases

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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/\ /\ =\ /"
Enter fullscreen mode Exit fullscreen mode

If this article helped you, I have a lot more tips on simplifying writing software here.

Top comments (16)

Collapse
 
ironsavior profile image
Erik Elmore

git rekt

git config --global alias.rekt = "reset --hard"

Collapse
 
diazsasak profile image
Diaz Guntur Febrian

why i got this error?

expansion of alias 'rekt' failed; '=' is not a git command

Collapse
 
michi profile image
Michael Z

Check this comment dev.to/maxtruxa/comment/idja
You must omit the equals sign.

Thread Thread
 
diazsasak profile image
Diaz Guntur Febrian

thank you

Collapse
 
maxpou profile image
Maxence Poutord

oh I love this one!

Collapse
 
thecodingalpaca profile image
Carlos Trapet

I'm stealing this one for demacia

Collapse
 
moopet profile image
Ben Sinclair

I like "commit crime".

Collapse
 
maxtruxa profile image
Max Truxa

Nice post!

Some of the examples are broken.
The = is part of the config syntax but must not be present on the command line. For example

git config --global alias.c = "commit"

should be

git config --global alias.c "commit"
Collapse
 
michi profile image
Michael Z

Thanks for that! I corrected it

Collapse
 
robinbastiaan profile image
Robin Bastiaan • Edited

Awsome list.

Could the last one be simplified to git config --global alias.alias '! git config --get-regexp alias'? That seems to do the trick too. I do not understand the rest of the syntax so I might be missing something.

Collapse
 
4k1k0 profile image
Wako

$ git oops

checkout --

Collapse
 
cyberhck profile image
Nishchal Gautam

Instead of specifying branch name develop, which might get executed on another branch out of habbit, why not get current branch first? Search for ggpull and use its implementation :)

Collapse
 
srjturner profile image
Simon Turner • Edited

"Pushup" - to push commits from a (new) local branch to its (new) upstream:

git config --global alias.pushup "! git push --set-upstream origin \$(git branch | grep '*' | cut -d ' ' -f2)"

Collapse
 
codebrotha profile image
Tineyi Takawira

Nice! Thanks for this post.

Collapse
 
victoryarema profile image
Victor Yarema

It is sometimes recommended to use "pull --rebase=merges" instead of just "pull --rebase".

Collapse
 
opshack profile image
Pooria A

This is really good stuff! I would just make sure to not use git nah if I already pushed my code to the upstream.