Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 21, 2022 07:40 pm GMT

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

Alternatively we can create an alias

git config --global alias.st status

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

git st

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 branchgit config --global alias.ci commitgit config --global alias.co checkoutgit cofnig --global alias.pr pull --rebasegit config --global alias.pf push --forcegit config --global alias.rsh reset --hard

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"

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"

And how to use it.

vishalr@ubuntu (bug-fix-branch) $> git br* bug-fix-branchmastervishalr@ubuntu (bug-fix-branch) $> git copd mastervishalr@ubuntu (master) $> git br* mastervishalr@ubunt (master) $>

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


Original Link: https://dev.to/vishalraj82/fun-with-git-aliases-4o48

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To