Create a gist now

Instantly share code, notes, and snippets.

The Ultimate Git Alias Setup

The Ultimate Git Alias Setup

If you use git on the command-line, you'll eventually find yourself wanting aliases for your most commonly-used commands. It's incredibly useful to be able to explore your repos with only a few keystrokes that eventually get hardcoded into muscle memory.

Some people don't add aliases because they don't want to have to adjust to not having them on a remote server. Personally, I find that having aliases doesn't mean I that forget the underlying commands, and aliases provide such a massive improvement to my workflow that it would be crazy not to have them.

The simplest way to add an alias for a specific git command is to use a standard bash alias.

# .bashrc

alias s="git status -s"

The disadvantage of this is that it isn't integrated with git's own alias system, which lets you define git commands or external shell commands that you call with git <alias>. This has some nice advantages:

  • integration with git's default bash completion for subcommand arguments
  • ability to store your git aliases separately from your bash aliases
  • ability to see all your aliases and their corresponding commands using git config

If you add the following code to your .bashrc on a system with the default git bash completion scripts installed, it will automatically create completion-aware g<alias> bash aliases for each of your git aliases.

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion                                                                                                                                                                

function_exists() {
    declare -f -F $1 > /dev/null
    return $?

for al in `__git_aliases`; do
    alias g$al="git $al"

    complete_func=_git_$(__git_aliased_command $al)
    function_exists $complete_fnc && __git_complete g$al $complete_func

The main downside to this approach is that it will make your terminal take a little longer to load.

My aliases

Here are the aliases I use constantly in my workflow. I'm lazy about remembering many other aliases that I've decided I should be using, which this setup is great for because I can always list them all using gla.

    # one-line log
    l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short

    a = add
    ap = add -p
    c = commit --verbose
    ca = commit -a --verbose
    cm = commit -m
    cam = commit -a -m
    m = commit --amend --verbose

    d = diff
    ds = diff --stat
    dc = diff --cached

    s = status -s
    co = checkout
    cob = checkout -b
    # list branches sorted by last modified
    b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

    # list aliases
    la = "!git config -l | grep alias | cut -c 7-"

See Must Have Git Aliases for more.


I have 3 aliases which are pretty helpful, maybe you'd like to include them?

rao = remote add origin
ac = !git add . && git commit -am
pushitgood = push -u origin --all


git init
git rao https://url-here.git
git ac "Message"
git pushitgood

and you have a working repo with set upstream. git ac is still very useful throughout development while the others are for fist time setup only.


That ac one is gold


These are quite useful to me, so I'll share them!

undo-commit = reset --soft HEAD~1

This one removes all the branches that have been merged to the current branch that you are in.
If you use this from master then all branches merged to master are deleted. This reading git branch easier.
clean-merged = !git branch --merged | grep -v \"\\*\" | xargs -n 1 git branch -d


A variation on pushitgood

po=!echo 'Ah push it' && git push origin && echo 'PUSH IT REAL GOOD'


I´ve git an issue when add this to the .bashrc. I got
bash: __git_aliases: command not found
the problem is the __git_aliases command will be loaded after first use of git auto-completion - after first git <TAB><TAB>. So you will need to force include this command. On my system this will be in /usr/share/bash-completion/completions/git
Just add include this file to the .bashrc like this:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
    . /usr/share/bash-completion/completions/git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment