Git is a popular, powerful tool, and possibly the most successful version-control system there’s ever been. Git’s power is evident from its command set. It currently consists of about 150 commands, from the common git-status to the obscure git-get-tar-commit-id. With each command supporting its own set of options, there’s a staggering amount to remember.

Fortunately, Git has a way of simplifying things. A Git alias can act as a shortcut for any subcommand, with any set of options. With external commands, you can use even more powerful invocations. Here are 10 of the most useful aliases you can set up.

How to Set an Alias

Setting an alias is as simple as following this pattern:

        git config --global alias.co 'checkout'
    

The --global option sets the alias for all Git usage by the current user. To make an alias apply to all users of the system, use --system instead. To add an alias that’s specific to the current repository, use --local.

If you know where a config file is, you can edit it directly. System configuration is in a global location, typically /usr/local/etc/gitconfig. Your user-specific config will live in a file in your home directory such as ~/.gitconfig. Local configuration exists within each repository itself, in the .git/config file.

See also: How to Install and Configure Git on Linux

1. An Alias to Switch Branches Quickly

Depending on your workflow, you could be switching branches many times throughout each work session. To alleviate a small amount of effort, and practice with the simplest kind of alias, try the following:

        alias.co 'checkout'
    

Typing git co feature1 will now switch to the feature1 branch. The alias shortens the name of the subcommand, which continues to work with extra arguments.

2. View Condensed Status

By default, the git status command produces verbose output. It explains the state in detail, with useful information for anyone unfamiliar with Git. However, if you want to save some space and you're already familiar with Git terminology, you might want to set up an alias for a shorter version:

        alias.st status -sb
    

This is just a very slight variation on git-status, but the -s option produces a short output which makes things a lot more brief. It will take something like this default git status:

A screenshot of a terminal showing output from the git status command

And produce this instead:

A screenshot of a terminal showing output from a git status alias command

The -b option shows branch and tracking info which -s would otherwise suppress.

3. Show Summary Logging

Git offers many ways of inspecting a project’s history via the git-log command. It can filter the commits it reports and can display lots of different data associated with each commit. Sometimes, however, you want a log that’s as compact as possible. The --oneline option provides this, but it’s a lot quicker to alias it, given that it’s one you might use pretty often:

        alias.ll 'log --oneline'
    

The output presents one commit per line, with the title of the commit message, and an abbreviated hash. It looks like this:

A screenshot of a terminal showing output from a git alias of the log command

4. Undoing the Last Change

The git reset command is valuable, but it’s not the easiest to understand. Sometimes, you just want a simple undo which winds back the 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.

        alias.undo 'reset --soft HEAD~1'
    

5. Log of the Last Commit

If you’re picking up a project from the day before, it can be useful to examine the last commit. With a git-log alias you can get a quick, detailed report of the last commit:

        alias.last 'log -1 HEAD --stat'
    

The -1 HEAD option simply requests the very last commit and --stat lists the files that the commit affected, with the number of lines inserted and deleted for each.

A screenshot of a terminal showing output from a git alias for the log command

6. An Easy One-Shot Commit

You probably find yourself adding files and committing them in the very next step fairly often. Unless your workflow is more complicated, you might want to simplify this process with an alias:

        alias.ac '!git add -A && git commit'
    

The individual components don’t require much explanation, apart from noting that the -A option passed to git-add will automatically update the index so that all files match the working copy. It caters to file deletion, modification, and creation.

An interesting thing to note about this alias is that it combines two commands. It does so using the ! symbol as the first character of the alias. This informs Git that what follows is an external shell command rather than a subcommand.

7. Fancy Branch Formatting

Another command with lots of flexibility, git-branch can format its output in many ways beyond the sparse default.

        alias.br "branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate"
    

Examine the format from start to finish. Note that it includes lots of special fields. %(HEAD) adds an asterisk alongside the current branch.

A screenshot of a terminal showing output from a git alias for the branch subcommand

Aliases are a great way of experimenting with formatting for commands like git-branch. You’ll develop preferences over time, so keeping track of which options produce your favored output is a must.

8. Summarizing Changes By Contributor

A variant of git log, git-shortlog groups the commits it displays by author. This is ideal for release notes or simply keeping an eye on who’s done what recently.

A few options make for a very convenient all-purpose author summary perfect for regular use via an alias. The -e option shows email addresses in addition to names. The --summary option just outputs a total count rather than the subject of each commit. And --numbered orders the final output by the total number of contributions. You can use this alias:

        alias.contrib 'shortlog -e --summary --numbered'
    

To produce the following output:

A screenshot of a terminal showing output from a git alias of the shortlog subcommand

9. List Branches Sorted by Last Modified

Now for some more work with branches, this time using a lower-level subcommand. This complicated alias is another example of an external command—a pipeline, in this case. First, the git-for-each-ref command loops through all known branches. It does so in authordate order and formats the output to show that date alongside the commit hash. Finally, it uses sed to strip refs/heads/ from the name of each branch.

        alias.b '!git for-each-ref --sort="-authordate" --format="%(authordate)%09%(objectname:short)%09%(refname)" refs/heads | sed -e "s-refs/heads/--"'
    
A screenshot of a terminal showing output from a git alias using the for-each-ref command

See also: These Sed Examples Will Make You a Linux Power User

10. An Alias to Show All Aliases

With all this support for aliases, it’s surprising that Git doesn’t offer an easy way to view all the aliases you’ve set up. Don't worry, though, you can fix that with an alias! The git config command lists all current configurations. You can filter values using the --get-regexp option, so the following alias gives you a useful git alias command:

        alias.alias 'git config --get-regexp ^alias.'
    
A screenshot of a terminal showing output from a git alias to show aliases

Save Time and Frustration With Git Aliases

Git aliases save you time by eliminating the need to type long and complicated commands. Git has a huge number of subcommands but aliases are easy to reuse and maintain. You can even bundle aliases into your project via local configuration, allowing you to share shortcuts and standard practices.

Just like Linux aliases, Git aliases let you type less and do more. Try experimenting with the aliases above, and explore the full Git command set to see the wealth of functionality available.