DEV Community

Dev Shah
Dev Shah

Posted on

Assign alias to your long togo git commands!

The real hero who saves the life of a developer, when the code goes haywire, is none other than Git. It provides us with a facility where developers can experience the code with confidence without worrying about messing up the production code. It eliminates all the tangled webs from developers' lives and provides a smooth sailing experience through the development sea!!!

We use this savior almost every working hour to track and commit all the progress! We all know that there are certain keywords that we use a lot in the same sequence irrespective of the project or situation. For me, they are ["git commit -m "commit message"", "git remote add origin "link to remote repo"", "git checkout -b "branch_name"", "git push origin Master", "git pull origin Master"]. However, instead of using such long strings or keywords, why don't we just compress this into a few characters? Sounds fun? Would not only save time but also make the operation convenient, right?

Absolutely!! so let's do this. This operator is known as creating an alias. Alias literally means an alternate name. Hence, on the root level, we are just assigning all these commands their alias which we can later use to execute the whole long command. Here is the way you can give an alias to your togo git command.

git config --global alias.[command alias] 'long command excluding 'git''
Enter fullscreen mode Exit fullscreen mode

Description:
The 'command alias' becomes an alias of the long command. You have any keyword here except the stored git keywords. The long command can be written in inverted commas after the alias. One thing to make sure of here is to not include the keyword git itself. For example, if you are setting an alias for the command 'git push origin Master', you should pass 'push origin Master' while setting the alias. This is because even after creating an alias, we need to use the git keyword to let the terminal know we are using some git command. Once the alias is set, it can be called using 'git [command alias]'. The git will just replace this alias with the command assigned to that alias. Hence, in our case, if you set the alias of 'push origin Master' to 'ph', the git command call 'git ph' will replace the keyword 'ph' with 'push origin Master'.

Alright!! Now we're all set to rock the git and github.

Lastly, here are some of the aliases that I configured in my git to make my life easy. If there are some commands among the following that you use regularly, we can just copy and paste my commands to use the same alias as mine.

git config --global alias.rmt 'remote add origin'
Enter fullscreen mode Exit fullscreen mode

use: git rmt "link to the remote repository"
description:
Add a remote repository to the git.
By default, it names the remote repository 'origin'.

git config --global alias.bh 'checkout -b'
Enter fullscreen mode Exit fullscreen mode

use: git bh "branch name"
description:
Create and checkout to a new local branch.

git config --global alias.cm 'commit -m'
Enter fullscreen mode Exit fullscreen mode

use: git cm "Commit message"
description:
Commit the staged files with a commit message.

git config --global alias.ph '!f() { branch="${1:-Master}"; git push origin "Master:$branch"; }; f'
Enter fullscreen mode Exit fullscreen mode

use: git ph || git ph "remote branch"
description:
To push the code from the local Master branch to the remote Master branch.
By default, it pushes the code from the local Master branch to the remote Master branch. To push the code to some other remote branch, the name of the remote branch can be passed as an argument.

git config --global alias.pl '!f() { branch="${1:-Master}"; git pull origin "$branch"; }; f'
Enter fullscreen mode Exit fullscreen mode

use: git pl || git pl "remote branch"
description:
To pull the code from the remote Master branch to the local Master branch.
By default, it pulls the code from the remote Master branch to the local Master branch. To pull the code from some other remote branch, the name of the branch can be passed as an argument.

No need to thank me, instead Buy me a coffee!

Top comments (0)