Published on

Using Git aliases

You can create Git aliases to automate frequently used Git commands and sequences of commands. In effect, you can create your own Git commands.

Adding an alias

You can add aliases directly from the command line, but I find it easier to add them to the .gitconfig file so that I can tweak them more readily.


(Note: These instructions work on both Mac and Linux.)

  • Use a text editor to open the .gitconfig file located in your Home directory.

  • Add a section to the file called [alias].

  • Add an alias definition to the new [alias] section using the following format:

    <alias-name> = !git <git-commands or already-defined-alias or cli-commands>

    For example, the following alias will return a list of the most recent 15 commits, nicely formatted with the short version of the hash, the user name, a time stamp, and the commit message:

    lg15 = !git log -15 --pretty=format:"'%h - %an, %ar : %s'" && git status

    You can also use a variable in the alias definition to add more flexibility to your command:

    lg = !git log -$1 --pretty=format:"'%h - %an, %ar : %s'" && git status

    In this example, using a variable ($1) enables you to specify on the command line the number of commits you want to list after the git lg command: for example, git lg 10 lists the 10 most recent commits.

  • Save the .gitconfig file.

  • In a terminal window, change directories to your Git repo.

  • Run the alias from the command line:

    git <alias-name> Or if your alias uses variables: git <alias-name> <variable-text>

Sample alias definition

I’ve included some sample aliases below that you can copy/paste as text directly into your .gitconfig file. The comments above each alias definition provide descriptions, and usage instructions.

  • Adds any currently unstaged changes to the last commit you made. Good for situations where you just saved a minor tweak to a file and don't want to do an extra commit that you'll just have to squash later.

    # Usage: git include
    include = !git commit --amend --no-edit
  • Returns a list of the most recent β€˜n’ number of commits formatted with the short hash, the user name, a timestamp, and the commit message

    # Usage: git lg <number-of-commits>, e.g. β€˜git lg 15’ returns the most recent 15 commits and then gives you the status of your local repo.
    lg = !git log -$1 --pretty=format:"'%h - %an, %ar : %s'" && git status
  • Returns a list of the aliases (along with their definitions) that are currently defined in your .gitconfig file. Hooray for regex and sed!

    # Usage: git aliases
    aliases = !git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /
  • A simple undo command that winds back your last commit. The --soft option ensures that Git only removes the last commit without changing anything about the local files in your working tree. HEAD~1 is simply a way of referencing the commit one before the HEAD.

    # Usage: git undo
    undo = !git reset --soft HEAD~1
  • Provides a quick, detailed report of the most recent commit. Lists the files that the commit affected along with the number of lines inserted and deleted for each.

    # Usage: git last
    last = !git log -1 HEAD --stat
  • Returns a fancy-schmancy looking list of local branches including lots of special fields. %(HEAD) adds an asterisk alongside the current branch

    # Usage: git br
    br = !git branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate

Additional resources