What are Git aliases and how to configure & use them.
Git is a free and open source version control system designed to handle everything from small to very large projects with speed and efficiency.
One of Gitās features allow you to create aliases so youāre able to type shorter commands & combine multiple commands into one.
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 C:\Users\<user_name>
.
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:
[alias]co = checkout
Which tells Git to replace co
with 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:
[alias]list = ls
Running git list
would make Git get mad at you (see what I did there!)
$ git listexpansion 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:
[alias]list = !ls
Now typing 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.
Branches
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 .gitconfig
[alias]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 upstream/develop
š
How can I pass extra info, like a branch name, to my alias?
Basically something like this:
[alias]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.
[alias]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.
You can follow me on Twitter & GitHub.
Full list
So without further ado hereās my current list of aliases:
My current list of Git aliases