Contents

Alias in Git Bash Console

Detailed explanation on how to setup shortcuts in git console

Website Visitors:

What is an Alias

Alias are shoutcuts that you create to commands and run them in console. In Git console you can create such commands with alias keyword and use them in the console when you need to run that command.

Creating Alias

In your terminal, you have to specify alias AliasName=command to create an alias. Ex: alias gm=git checkout master. Drawback in using this alias command directly on git console is, if you close the console that alias will also be removed.

To overcome this issue, you have to use that same alias command in bash_profile file as explained below.

If you are using windows machine, goto c:\users\USERNAME\ folder and create a file called .bash_profile. If you are using a linux machine, goto your home folder which is /home/userid/ or just run cd ~ in your terminal to move to your home directory and create a file called .bash_profile or .bashrc.

open that file and add below command to create alias.

alias gm=git checkout master

That’s it. From now on, in your git console when you run gm command it will run git checout master command. You can also verify the alias by running alias command in the console. It will list all the aliases set on the machine.

For example, if you want to run a local hugo server, you’d normally run the command, hugo serve -e production --bind x.x.x.x. Using this, you can access your hugo site using that ip address:1313 on your local network devices. If you want to set a shortcut for this, you can set as,

1
2
3
4
5
6
7
alias hugostart='hugo serve -e production --bind x.x.x.x`

# If you do not have a static ip, then you have to modify that ip value in above command every time you reboot your machine.
# So to pick your machine ip, and set it in the command, use below code.

LOCAL_IP=${LOCAL_IP:-`ipconfig.exe | grep -im1 'IPv4 Address' | cut -d ':' -f2`}
alias hugostart='hugo serve -e production --bind $LOCAL_IP'

Removing Alias

If you’d like to remove an alias that you’ve set, you have to use the command unalias followed by the alias name. Ex: unalias gm. This will remove the alias called gm from your machine.

Suggested Article

If you’d like to continue reading, check our gmail or protonmail article here or browse all our topics here.

Conclusion

In this article, we’ve explained how to create an alias in git. We’ve also demonstrated examples for all the topics that are discussed in the article.

We hope you have learned something new in this article. All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest.

Please feel free to share your thoughts about this article in the comments section below.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.