As developers, we know Git very well, as it is a very important part of our daily activity. Software developers use it all the time. We can not spend a day without interacting with Git. We can run Git from the terminal or use some third party tools like Sourcetree.

But there are some terminal fans who always love to run Git from the terminal only. So for them, it is sometimes difficult to remember and write those long commands. Ohh no buddy!! It is very much a boring and time-consuming task to write long commands all the time ???.

So what should we do now???

Okay, we should start looking for a shortcut for those long long commands.?‍?‍?‍

Look what we found: Git Alias. It has come as the rescuer for all.

We all likely know what an alias is — it means a false name or nickname.

So using git alias, we can assign a nickname to a long git command. This is perfect. ?

Now let’s try to find a place where we can write these nicknames.

Searching ? Searching ?Searching ?…

Yes, bash_profile is the place where we can write them.

How to open bash_profile ?

From Terminal, we can easily open bash_profile by using the following command:

vim ~/.bash_profile

Now enter into insert mode in your vim editor by tapping i from the keyboard.✓

Create your first alias in bash_profile:

The first program we use to write in any programming language is a Hello World program. Let’s not break this tradition — we are going to write our very first alias with a simple hello command.

Open bash_profile, and write the following line:

alias hello="echo Hello Boudhayan!! How are you?"

It says that we have created an alias named hello and assigns the right-hand side as the command to execute. So whenever we write hello in the terminal, it should execute the command assigned to it.

Save the changes and reload the bash_profile by using the following command:

source ~/.bash_profile

Now if we type hello in the terminal, it prints Hello Boudhayan!! How are you?

1*S7sKAFEQuZellxzd1G9L3w

Awesome!! ???

So we have learned how to create an alias command in bash_profile.

If we look closely, then we can relate to it. We can find some similarities with the variable declaration in any language. Yes, we already know about that, right?

Coming to the main topic

Now let’s create some git aliases to make our daily life easier and faster.?

git clone

We use this command for cloning a remote repository to a local system.

Though it’s a short command, we want to start learning git aliases by making it even shorter.?‍

Just like above, open bash_profile, write the below line and reload bash_profile. See the magic.☄️

alias gc="git clone"

So from now on, for cloning a repository, we do not need to write this:

git clone <remote repository url>

instead, we will use the below command for cloning purposes:

gc <remote repository url>

Boom!! Your remote repository is successfully cloned into your local system.???

Create some more aliases

We push our local commits to the development or master branch by using the below commands:

git push origin develop
git push origin master

Now, we can write shorter version like below:

alias gpd="git push origin develop"
alias gpm="git push origin master"

So from now, we will use gpd and gpm to push local commits to the development and master branch respectively.

?????? Hurrah!! 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 test

It will create a directory named test in the current path and move to that directory. Cool!!?

Advanced Git Aliases

To 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!! ???