Some useful git aliases and how to set them up
Visual Studio Code is my editor of choice and I usually use its Source Control features to create a branch, switch branch, push and pull in git. Hold your horses before cringing about me using GUI instead of the command line.
I don’t usually care whether I’m using command line or GUI to achieve a task — as long as the tool of choice helps me achieve it faster with the least amount of friction. This means that I still need to use git commands from command line from time to time when I feel it will help me achieve my task faster.
Building upon the same line of thought, I prefer to use aliases instead of typing entire git commands. Eg: I would use gbr
instead of git branch
to list the branches of my git repo and to see the current branch.
Below is the entire list of git aliases of the git commands I use on a day-to-day basis:
gbr -> git branchgps -> git pushgpl -> git pullgcm -> git commit -mgcam -> git commit -a -mgst -> git statusgdf -> git diffgad -> git addgch -> git checkoutgchb -> git checkout -b
You can use your own aliases or modify these according to your needs. Here’s a gist which gives aliases for more git commands: https://gist.github.com/tj/4998585
Setting these aliases up is pretty easy too. You just need to run the command below:
alias yourAlias="long-command-that-you-want-to-shorten"
You would ideally want these aliases to be available everytime you start a terminal session. For that, you need to put your alias
commands inside ~/.bashrc
for Linux/Ubuntu. Follow the following steps to achieve this:
- Open up
~/.bashrc
usingvim ~/.bashrc
- Add your aliases at the end of the file:
alias gbr="git branch"
alias gps="git push"
alias gpl="git pull"
alias gcm="git commit -m"
alias gcam="git commit -a -m"
alias gst="git status"
alias gdf="git diff"
alias gad="git add"
alias gch="git checkout"
alias gchb="git checkout -b"
3. Save your file and run source ~/.bashrc
to reflect the new aliases immediately in the current terminal session. Otherwise, you would have had to close the terminal and open it again for the aliases to be available to you.
4. Now, you’ll be able to use gbr
,gchb
, etc. instead of their verbose counterparts.
Protip: You can use alias
command to alias any long command you want, and not just git commands. I also use aliases for SSH commands. For example, below is a sample alias that I use to SSH into one of my servers:
alias ssh-srv="ssh -i srv-aws-key-pair.pem ec2-user@ec2-10-0-0-0.ap-southeast-1.compute.amazonaws.com"
After this, I just run ssh-srv
to SSH into my server.