How to Set Up Git Aliases
Git aliases provide you with the freedom to choose which command to enter for a specific Git operation. This can be helpful for typing fewer characters when executing frequently used commands, creating alternative versions of commands that you tend to mistype, or simplifying lengthy and complex operations.
Setting up a Git alias is a straightforward process, and we recommend creating new aliases regularly as you become familiar with new commands that you find yourself using frequently.
Let's start with a very basic example. You probably type git status
often. Wouldn't it be great if you could simply type git st
?
To achieve this, all you would need to do is type the following:
$ git config --global alias.st status
Done! You've just created your first alias!
If you now run git config --global --edit
, you should see a new entry added at the end of the file. You can, of course, add more aliases by simply editing this configuration file instead.
[alias]
st = status
At any point you can type git help <alias>
to learn more about your alias:
$ git help st
'st' is aliased to 'status'
Finally, you can also type git config --get-regexp alias
to view a list of all the aliases you have created.
Useful Aliases Worth Setting Up
We recommend you start by creating aliases for the most popular Git commands. Here are some suggestions:
[alias]
a = add
b = branch
cm = commit -m
cl = clone
co = checkout
pu = push
pl = pull
st = status
You can also take a moment to create short aliases for long commands, such as Force Push with Lease or Amend, or come up with aliases for more specific commands that you think should be set as the default behavior in Git, like the latest
command below.
[alias]
pf = push --force-with-lease
amend = commit --amend --no-edit
latest = log -5 --oneline
Shell Aliases
You can even configure Git aliases to run external commands, instead of a Git subcommand. You just need to add a "!" before the command, like in the following example:
[alias]
hey = "!echo hey"
For example, if you use gitk
to visualize commits, you can create an alias:
git config --global alias.visual '!gitk'
Now, simply run:
git visual
and GitK will open automatically.
This can also be useful for chaining different Git commands (using &&
) or for adding parameters (using $1
, $2
, etc.).
Advanced Git Aliases (Creating Missing Commands)
Some Git commands feel like they should exist natively, but they don’t. Fortunately, aliases allow us to create them.
Here are some examples.
1. Fixing Git’s Usability Gaps
Git does not provide a built-in unstage
command to remove files from staging.
However, you can add one:
git config --global alias.unstage 'reset HEAD --'
Now, these two commands are identical:
git unstage <file>
git reset HEAD -- <file>
Another example is viewing only the latest commit, which Git doesn’t provide a simple command for by default:
git config --global alias.last 'log -1 HEAD'
Now, git last
will show your most recent commit instantly:
git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <dreamer3@example.com>
Date: Tue Aug 26 19:48:51 2008 +0800
Test for current head
2. Wrapping Multiple Commands into a Single Alias
Aliases can also execute multiple Git commands in one step. For example, cleaning up merged branches:
[alias]
cleanup = "!git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d"
Now, instead of manually checking and deleting merged branches one by one, you can just run:
git cleanup
3. Aliasing Git Log for a Cleaner History Graph
Git’s default git log
command can be overwhelming. This alias makes it more readable:
[alias]
graph = !"git log --all --graph --decorate --oneline"
Now, simply run:
git graph
This provides a clearer commit history visualization directly in your terminal.
Managing & Troubleshooting Git Aliases
Where Are Git Aliases Stored?
Git aliases are stored in configuration files depending on their scope:
- Global aliases: Stored in
~/.gitconfig
(applies to all repositories). - Local aliases: Stored in
.git/config
inside your repository (applies only to that repo).
To open your global Git config for editing:
git config --global --edit
Two Methods to Create Aliases
Using git config
(Fast but Limited)
The easiest way to add an alias:
git config --global alias.co checkout
This updates the global config file automatically.
Editing the .gitconfig
File Manually (Useful for Bulk Editing)
If you prefer to set multiple aliases at once, you can edit your global config file directly:
[alias]
co = checkout
br = branch
ci = commit
st = status
This is often faster if you want to define many aliases in one go.
Viewing All Defined Aliases
To see all aliases you've configured:
git config --get-regexp alias
Removing an Alias
To delete an alias (e.g., git st
):
git config --global --unset alias.st
Troubleshooting Common Issues
- "My alias isn't working!"
Run git config --get-regexp alias
to check if it's defined correctly.
- "Alias deletions aren’t applying!"
Ensure you removed the alias both globally (--global
) and locally if needed.
- "How do I make an alias run a shell command?"
Prefix the alias with !
, like:
[alias]
open = "!echo 'Opening project...'; cd ~/Projects"
Now, git open
will print a message and switch to the Projects
directory.
Final Words
Git aliases are one of the best ways to speed up your workflow, reduce typing fatigue, and improve usability.
Try implementing some of the aliases suggested above and see how they simplify your workflow!