How can I alias a git command?

  • Page Owner: Not Set
  • Last Reviewed: 2018-09-13

I'd like to make some aliases for my common git log commands, how can I do it?


Answer

There are two ways. First, find your gitconfig file, it's typically located in your users home folder (~/.gitconfig) and create a block called alias (if one doesn't exist already). Inside, you can put any alias that you want. For these to work the command has to be a git command (git checkout could become git co by creating the alias co = checkout). Here is my list of git aliases:

[alias]
        co = checkout
        count = !git shortlog -sn
        co = checkout
        ci = commit
        st = status -sb
        br = branch
        hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
        type = cat-file -t
        dump = cat-file -p

Alternatively, if you don't want to edit the gitconfig file directly, you can use the git config command to save the aliases for you. Here is how you'd add git co using the git config command:

$ git config --global alias.co checkout