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"
This can be useful for chaining different Git commands (using &&
) or for adding parameters (using $1
, $2
, etc.).