8 Amazing Aliases To Make You More Productive With Git
Type less, develop faster. All through the power of git aliases
You use git every day, so you should be able to use it efficiently. A good way to increase efficiency is to reduce the typing effort. This is where aliases come into play. Aliases for shell commands are common, and so they are for git.
You can create an alias for git the usual Linux way, but git comes with its own approach. You can use the aliases on Windows too.
How To Create a Git Alias
Execute the following command to create an alias:
git config --global alias.alias_name git_command
To run external programs or concatenated commands with git aliases, just prefix the command with an exclamation mark:
git config --global alias.visual '!gitk'
It is crucial to use single quotes here. The exclamation mark itself is a shell command which repeats the last command you used. Using single quotes prevents the shell from executing any commands inside the quotes.
That’s it! Now let us look at some helpful aliases.
The Aliases
1. Abbreviations — the usual suspects
Shortening long commands is obvious. In fact, this is the first thing the documentation mentions. Instead of typing git commit
, you could just type git ci
. Four letters saved. Here are the self-explanatory aliases from the documentation:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
2. A better git log
git log
shows you all commits made to the repository. By default, it is very verbose and unclear.
With the following alias, it will look way better.
git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit"s
Well, that saved some typing, didn’t it? Here is the result:
3. List all branches
This alias gives you a comprehensive list of all local and remote branches:
git config --global alias.bra "branch -a --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:blue)(%(committerdate:short)) [%(authorname)]' --sort=-committerdate"
Which looks like this:
4. git f*ck it
Sometimes you realize that your current work leads nowhere, so you may decide to undo it all. This alias helps you by resetting everything and removing untracked files.
git config --global alias.fit '!git reset --hard && git clean -fdx'
5. Uncommit
Commited too fast? Unless you did not push your commit, the following alias will help. It removes the commit but keeps the changes.
git config --global alias.uncommit 'reset --soft HEAD^'
6. Add and commit in one go
This will add all changes and opens the commit message dialog:
git config --global alias.ac '!git add -A && git commit
7. An alias to rule them all
Forgot which alias you created? There’s an alias for that, as shown below:
git config --global alias.alias "config --get-regexp ^alias\."
8. See the last commit
This comes in handy if you just need to know what happened recently.
git config --global alias.last '!git --no-pager log -1 HEAD'
Recap
Git aliases help you master git while typing less. Check out the chapter on Aliases from Pro Git; it’s free!
Do you have a cool alias that I did not mention? Please write it in the comments!
Shoutout to unirest-java; it’s the repository I used in the screenshots.
Thank you for your time!
git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit"s
^ extra s at the end :)
cool.
I never relied on git aliases. But I think they might be helpful in certain cases when you have many flags on frequently used commands.
Not an alias. But 'git add -i' for interactive staging is probably the most powerful command that I often use. Also git bisect for debugging.