DEV Community

Vishal Raj
Vishal Raj

Posted on

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.

Top comments (4)

Collapse
 
sinewalker profile image
Mike Lockhart

Show remote URL:

url = config --get remote.origin.url

and also, blow up the local (edited to be without swearing...):

screw-this-noise = !echo 'Well, screw this noise!'; NOISE=$(git url); THIS=$(basename $PWD); cd ..; rm -rf $THIS; git clone $NOISE; unset THIS NOISE

and my favorite, push to a merge request (GitLab, but works on GitHub too):

pmr = !git push --push-option=mergerequest.create --set-upstream origin $(git branch|awk '/\*/{print $2}')

Collapse
 
vishalraj82 profile image
Vishal Raj

Whatever you wish my pal !!

Collapse
 
j0etheripper profile image
youssef alaa

noice

Collapse
 
ferminmoli profile image
Fermín Molinuevo

awesome! thanks!