What are Git aliases and how to configure & use them. Git is a free and open source [version](https://hackernoon.com/tagged/version) control system designed to handle everything from small to [very large projects](https://blogs.msdn.microsoft.com/bharry/2017/05/24/the-largest-git-repo-on-the-planet/) with speed and efficiency. One of [Git](https://hackernoon.com/tagged/git)’s features allow you to create [aliases](https://hackernoon.com/tagged/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](https://hackernoon.com/tagged/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 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: \[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: 1. Set a remote upstream branch with the _same name_ as your current branch, 2. 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](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html). 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](https://www.atlassian.com/git/tutorials/comparing-workflows/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](https://git-scm.com/book/en/v2/Git-Basics-Working-with-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](https://twitter.com/Yo_Gad) & [GitHub](https://github.com/Yohanna). #### Full list So without further ado here’s my current list of aliases:  My current list of Git aliases ### Obligatory Git joke  [https://xkcd.com/1597/](https://xkcd.com/1597/)