Useful Aliases for Git Commands

Git is one of the programs that I use almost every day. If you have not noticed already, git commands tend to be moderately long, and some commands are so regularly used that they become tedious to type out. One of the most common commands is git commit ..., and this alone takes at least 10 keystrokes. With a bash (or zsh) alias (e.g. gc), lots of typing can be saved, especially if the commands are used multiple times throughout the day. This article will document the aliases I have in my shell's configuration file (i.e. .bashrc or .zshrc), and I hope that they can be useful to you in reducing the number of keys that you have to press.

Here are the bash aliases for git to help you get started. They include aliases for git, git status, git add, git commit, git diff, git log, etc:

# Aliases for git.
alias g='git'
alias gs='git status'

alias ga='git add'
alias gaa='git add --all'

alias gc='git commit'
alias gcm='git commit --message'
alias gca='git commit --add'
alias gcam='git commit --add --message'
alias gce='git commit --amend'  # Edit commit message. 'gce' = 'git commit edit'

alias gb='git branch'
alias gba='git branch --all'

alias gd='git diff'
alias gds='git diff --staged'
alias gdt='git difftool'

alias gmt='git mergetool'

alias gch='git checkout'
alias gchm='git checkout master'
alias gchb='git checkout -b'

alias gl='git log --decorate'
alias glo='git log --decorate --graph --oneline'
alias gloa='git log --decorate --graph --oneline --all'
alias glao='gloa'
alias gls='git log --decorate --graph --stat'
alias gla='git log --decorate --graph --all'
alias gld='git log --decorate --graph --patch'  # Show diff. 'gld' = 'git log diff'
alias glda='git log --decorate --graph --patch --all'
alias glad='glda'

alias gp='git push'
alias gpo='git push origin'
alias gpom='git push origin master'

alias gri='git rebase --interactive'
alias grc='git rebase --continue'

Place these aliases in ~/.bashrc (or ~/.zshrc if you're using ZSH) to make them available in every shell session. Yes, it's true that git has facilities for defining 'aliases' in the .gitconfig file, but I prefer defining shell aliases instead, because they follow a format that is the same as all the other shell aliases that I already have (i.e. no need to bother with one more syntax for defining aliases). Also, if I have the need to define shell functions for git in the future, all the aliases and functions can go into the same shell configuration file, making maintenance easier.

The aliases for git commands shown above are almost the same as those I have in my configuration file. They have saved me lots of keystrokes, and I hope they make a good starting point in saving you lots of keystrokes too.