What are Git aliases and how to configure & use them.
For example, I have an alias for the
checkout command to
co so you can type
git co instead of
git checkout (Yea, I’m that lazy), Git simply replaces the new command with whatever you aliased it for.
Over time, I’ve created & collected useful aliases that made my everyday work with Git easier. I’ll be going through a couple of them below.
Configuring a new alias
First let’s talk about how to configure aliases.
You can easily set up an alias for a command using
git config. Here are a couple of examples you may want to set up:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.st status
This basically adds a new entry in
~/.gitconfig file. Git uses this file to store all your settings. On Windows it’s located in
Another way to add a new alias would be to edit
~/.gitconfig directly. I always use this method since I can add comments to my aliases.
To do that, simply open
~/.gitconfig in your current favourite editor, and add the new alias under the
[alias] section. For example, to add
git co manually:
co = checkout
Which tells Git to replace
checkout everytime you write
git co .
Keep in mind that this would only work for Git subcommands (like checkout, status, commit). For instance, If you try to alias an OS command:
list = ls
git list would make Git get mad at you (see what I did there!)
$ git list
expansion of alias 'list' failed; 'ls' is not a git command
Aliasing external commands
Fortunately Git provide a simple way to alias anything that’s not a git command. If you want to run an external command, start the command with a
! character. Using our previous example:
list = !ls
git list in the terminal would instruct Git to run the
ls command & list files in current directory.
I’ll use two aliases I use daily to show you how powerful Git aliases are.
After creating a new branch, you’ll want to push it to your remote fork of the repo to have a backup of your code or to be able to merge it later with other repos.
If you type
git push after creating a new branch, you’ll get an error similar to this:
fatal: The current branch <branch_name> has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin <branch_name>
Basically Git is telling you that you need to explicitly tell it which remote branch you want your local branch to track before you can push your changes. And you can run the command it provided to push the branch & set it’s remote at the same time.
Now I don’t know about you, but I don’t want remember all of this, and I don’t want to type
git push in order for Git to remind me of the command I need, so I can copy & paste it in the terminal to run. That’s just too much work :)
How about if we can just type one command that will:
- Set a remote upstream branch with the same name as your current branch,
- and then push your current branch.
To do that, we’ll create a new alias called
publish that does that for us. However, in order for
publish to work it needs a way to figure out the current branch name. Fortunately we can run this Git command:
git rev-parse — abbrev-ref HEAD to give us this info.
To be able to get the output of one command, and use it in another, we can use Command Substitution.
So now our
publish alias becomes:
git push --set-upstream origin $(
git rev-parse — abbrev-ref HEAD)
This is how it looks in
publish = git push --set-upstream origin $(
git rev-parse — abbrev-ref HEAD)
This is where Git aliases really shine, its ability to combine multiple commands to save you time.
Note that you’ll notice in my personal aliases list (at the end of this post) I’ve created a separate alias for the command that returns the current branch name, just for sake of organization.
Now let’s see how we can even create aliases that takes input from the user!
Syncing with upstream
One of the other really helpful alias I use constantly is:
git sync upstream/develop
If you work on open source projects or if you use the Git Forking workflow, then you’ll find yourself always updating your repo with the upstream repo (the repo you forked from).
This process basically involves two steps, fetching the commits you want to merge. And actually merging those two commits.
To do the first step I run
git fetch --all --prune when gets all the newer commits from all remotes I have.
And then I run
git rebase -p --autostash upstream/develop to merge the commits fetched specifically from
upstream/develop branch to my current branch. While these two commands can be combined in an alias, I found that sometimes I want to rebase commits fetched from a different branch than
How can I pass extra info, like a branch name, to my alias?
Basically something like this:
sync= !git fetch --all --prune && git rebase -p --autostash <user_input>
Fortunately that’s still doable in Git. All you need to do is add a
$1 to where your input will be used in the command.
sync= !git fetch --all --prune && git rebase -p --autostash $1
And now you can just call the
sync alias with any input you’d like!
Hopefully that gives you an introduction to Git aliases. You can find my list of aliases below.
If there are any other aliases that you use and find helpful, let me know and I’ll add them to the list :)
Thanks for reading — I hope you found this article useful.
So without further ado here’s my current list of aliases: