What are Git aliases and how to configure & use them. Git is a free and open source control system designed to handle everything from small to with speed and efficiency. version very large projects One of ’s features allow you to create so you’re able to type shorter commands & combine multiple commands into one. Git aliases For example, I have an alias for the command to so you can type instead of (Yea, I’m that lazy), Git simply replaces the new command with whatever you aliased it for. checkout co git co git checkout Over time, I’ve created & collected useful aliases that made my everyday work with easier. I’ll be going through a couple of them below. Git Configuring a new alias First let’s talk about how to configure aliases. You can easily set up an alias for a command using . Here are a couple of examples you may want to set up: git config $ 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 file. Git uses this file to store all your settings. On Windows it’s located in . ~/.gitconfig C:\Users\<user_name> Another way to add a new alias would be to edit directly. I always use this method since I can add comments to my aliases. ~/.gitconfig To do that, simply open in your current favourite editor, and add the new alias under the section. For example, to add manually: ~/.gitconfig [alias] git co [alias]co = checkout Which tells Git to replace with everytime you write . co checkout 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 would make Git get mad at you (see what I did there!) git list $ 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 in the terminal would instruct Git to run the command & list files in current directory. git list ls 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 after creating a new branch, you’ll get an error similar to this: git push 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 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 :) git push How about if we can just type one command that will: Set a remote upstream branch with the as your current branch, same name and then push your current branch. To do that, we’ll create a new alias called that does that for us. However, in order for to work it needs a way to figure out the current branch name. Fortunately we can run this Git command: to give us this info. publish publish git rev-parse — abbrev-ref HEAD To be able to get the output of one command, and use it in another, we can use . Command Substitution So now our alias becomes: publish 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 , then you’ll find yourself always updating your repo with the upstream repo (the repo you forked from). Git Forking workflow This process basically involves two steps, the commits you want to merge. And actually those two commits. fetching merging To do the first step I run when gets all the newer commits from all I have. git fetch --all --prune remotes And then I run to merge the commits fetched specifically from branch to my current branch. While these two commands can be combined in an alias, I found that sometimes I want to commits from a different branch than 😕 git rebase -p --autostash upstream/develop upstream/develop rebase fetched 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 to where your input will be used in the command. $1 [alias]sync= !git fetch --all --prune && git rebase -p --autostash $1 And now you can just call the alias with any input you’d like! sync 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 Obligatory Git joke https://xkcd.com/1597/