An intro to Git Aliases: a faster way of working with Git

We have made it.

????????????????????????I have created some more git aliases which can be really useful in our programming life.

Check them out:Shell Function:We can also use the shell function to declare more complex git aliases.

But to start with this, we need to know how to write a shell function.

????It is very easy to write a shell function which is like a normal C function.

????function function_name() { command1 command2 .


commandn}Now let’s try this.

This function will create a directory in the current path and then immediately move into that directory.

We know the below commands already to make it happen:mkdir <directory_name>cd <directory_name>We can compress those two commands by creating a simple function in bash_profile like below:function mdm() { mkdir -p $1 #here $1 is the first parameter to the function.

cd $1}Now reload the bash_profile source once and run the following:mdm testIt will create a directory named test in the current path and move to that directory.

Cool!!????Advanced Git AliasesTo push the code in the remote branch, we need to make a commit with some message.

Only then we can push to a branch.

So basically this is a combination of two commands (commit and push).

But we want to try the same with a single one-line command by writing a shell function for this.

????We can easily do this by writing a simple shell function.

Open bash_profile and write the following the function:function gcp() { git commit -am "$1" && git push }Reload the bash_profile once and use the command like below:gcp "initial commit"Cool!!.From now we can use this gcp command to commit and push in one shot.

????In a development or feature branch, all the team members push their changes almost every day.

So sometimes it is very difficult to find a particular commit among all the commits.

To easily handle this type of situation, we can write a function which will search the commit logs for a particular message and return the commit.

To do this, we will write a function like below:function gfc() { git log –all –grep="$1"}Occasionally if we want to search for a commit by the commit message, then we can do it by using gfc:gfc "<commit message>"Conclusion:So we have learned how to use shortcuts for git commands.

May these aliases and functions save you from writing those long git commands and make your life easy and smooth.

You can add your own aliases, functions and make modifications to them — no one’s permission is required except bash.

????????????????????????.Cheers!!!.Thank you for reading!!.????????????.

. More details

Leave a Reply