Fun with Git aliases

vishalraj82

Vishal Raj

Posted on January 21, 2022

Fun with Git aliases

Git is one of the most popular VCS (version control system). In this post we will discuss about git aliases and how to use them to their benefits.

So what are git aliases ? In simple words, aliases are custom shortcuts for executing regular git commands. These shortcuts are specified in the file $HOME/.gitconfig. Lets start with some examples.

In order to see the current status, the command would be

git status
Enter fullscreen mode Exit fullscreen mode

Alternatively we can create an alias

git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

And now, to see the git status we can run the command

git st
Enter fullscreen mode Exit fullscreen mode

Similarly, more and more aliases can be added for easier and quick execution of commonly used git commands. Lets add some more aliases.

git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.co checkout
git cofnig --global alias.pr pull --rebase
git config --global alias.pf push --force
git config --global alias.rsh reset --hard
Enter fullscreen mode Exit fullscreen mode

Well, until now we have seen creating aliases for simple git commands. But what if you want to run multiple git commands in single alias. Functions to the rescue.

Lets see an example situation. Let say you need to switch branch, but you also have staged changes. So you want to stash the changes before switching to new branch, and pull the latest changes as well. But for this, we will edit the file $HOME/.gitconfig and add new entry under the tab [alias]. I am a vim guy, but you can use any editor of your choice.

[alias]
    st = status
    br = branch
    co = checkout
    pr = pull --rebase
    pf = push --force
    rsh = reset --hard
    cop = "!f() { br=`git br --show-current`; git stash save \"Stash from branch ${br}\"; git fetch; git co ${1}};  f"
Enter fullscreen mode Exit fullscreen mode

The shortcut cop can also be extended. Lets extend the above case with situation that when you switch branch, you also want to delete the current branch. But why would you want so ? Lets say that you branched out from master for a minor bug fix. After you push the bug-fix branch to remote and submit it for review, you want to purge it from local, after switching back to master branch. Lets see

[alias]
    copd = "!f() { br=`git br --show-current`; git stash save \"Stash from branch ${br}\"; git fetch; git co ${1}; git br -d ${br}; };  f"
Enter fullscreen mode Exit fullscreen mode

And how to use it.

vishalr@ubuntu (bug-fix-branch) $> git br
* bug-fix-branch
master
vishalr@ubuntu (bug-fix-branch) $> git copd master
vishalr@ubuntu (master) $> git br
* master
vishalr@ubunt (master) $>
Enter fullscreen mode Exit fullscreen mode

Now that you have learnt, go ahead and create git aliases of your choice and have fun.

💖 💪 🙅 🚩
vishalraj82
Vishal Raj

Posted on January 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Fun with Git aliases
git Fun with Git aliases

January 21, 2022