Awesome Git Aliases
Git is an amazingly powerful tool. It can keep track of all the code you write, let you organize your work into different branches, help you seamlessly work with other developers, and even let you time travel and make changes.
But wouldn't it be awesome if Git could do more? What if you could customize it with your own commands, making it do anything you can imagine?
In this three-part series, I show you exactly how to do that. You'll learn how to create custom Git aliases you can use to run any command you'd like. I'll also show you many useful commands you can add to Git to make it even more useful.
This article is part of the No Bullshit Git Free Intro Course. If you're interested in Git and you want to catch the other lessons, be sure to sign up for the free course.
How to Alias Commands
Before we go any future, just what is an alias? An alias is a shortcut for running another Git command. You can set alias just like you would any other configuration value.
For example, let's say you often mistype
git status as
git stats, and you wanted
git stats to run the same command. You could do that by setting the
alias.stats value to
status like this.
git config --global alias.stats status
Boom! Just like that, you've created an alias.
Just like other configuration values, you can also open up your
~/.gitconfig file and add aliases there as well. This does the same thing as the command above.
[alias] stats = status
You likely use Git every day. Commands like
git add and
git commit are awesome.
But ugh. All that typing. Who has time for that?
Some of the most useful aliases you can set up for Git are one-or-two-character shortcuts to common commands.
[alias] a = add b = branch c = commit cl = clone co = checkout cp = cherry-pick m = merge p = push --follow-tags pu = pull r = reset s = status
These aliases can be huge time savers. Instead of typing
git status, you only need to type
git s. Instead of
git add ., you can type
git a .. It may sound trivial, but give it a try, and I promise you won't go back to typing out the full words.
Sometimes, you need to push changes up to a remote repository and overwrite files. You may be used to doing that with
git push --force.
But what happens if somebody else has already pushed changes to the same branch? Your command will wipe out their commits. That's no good.
Git has a safer way to push changes and overwrite your commits. Instead of using the
--force flag, you can use
--force-with-lease. This flag will prevent you from accidentally overwriting somebody else's commits. The way this works is a bit magical, but you can trust that it does.
You can write a
force-push alias for this:
git config --global alias.force-push "push --force-with-lease"
Now you can run
git force-push, which is a bit easier to remember. Of course, in the interest of saving a few keystrokes, you can also add a short version of this alias.
git config --global alias.fp force-push
Please note, you can only alias an alias in Git 2.20+.
There's another powerful addition to Git Aliases you can use: shell aliases. If you add a
! in front of an alias's value, you can run any shell command. For example, you can add the following alias to print out
hello every time you type in
git config --global alias.hello "!echo hello"
You know 'em, you love 'em—dad jokes. When you accidentally mistype
git add as
git dad, it's only appropriate that Git responds with a dad joke.
Note: I didn't come up with this idea. The earliest reference I could find to this idea a post on Reddit.
git config --global alias.dad '!curl https://icanhazdadjoke.com/ && echo'
Now why you accidentally type
git dad, you'll get a gem like this.
Why was the robot angry? Because someone kept pressing his buttons!
That's All for Now
That's for sticking with this article to the end! Hopefully, you've learned how to set up your own aliases, and you found some of the ones I included useful.
In the next part of this series, we'll be taking a much deeper dive into building custom Git commands that will speed up your workflow. Until then, have fun!
If you enjoyed this article and want to learn more about Git, be sure to check out the No Bullshit Git Free Intro Course.