Like most developers, one tool that I use consistently on a daily basis is git. While I am a pretty-fast touch typer, and very comfortable in the command line, I eventually discovered that I was spending a lot of time typing the same commands over-and-over again. Operating along the same lines at the DRY principle (don’t repeat yourself), I decided to find a better way, and that is when I stumbled upon aliases.
Aliases are bash commands that associate a string with another command. Aliases are defined like this:
$ alias ls="ls -l"
The above command will make it so that typing
ls will always list the contents of the directory on a separate line, with extra details. Basically, when typing
ls, anything between the quotations in your alias definition are executed.
Now, one issue with this is that if I close the terminal, I will lose this alias. In order to always have this available, I needed to add the alias to
.bashrc file in my home directory. Commands in this file are executed every time I start my terminal, thereby setting up all of my aliases.
So, leveraging the power of the
alias command, I decided to create some aliases for my most frequently used git commands. Below is a list of the aliases I have created.
gs, alias for
git statusfor a quick way to get my current git situation..
gato stage all of the modified/added/deleted files for commit (alias for
git add -A).
gcand then type a commit message to make a commit (
git commit -m).
gd, alias for
git diffto compare the current state with HEAD, or add commit hashes to compare commits.
gl, to list the log of commits. I like the one-line logs, so this is an alias for
git log --oneline.
gp, to push changes to the tracked branch, an alias for
gu, to pull changes from the tracked branch (
gm <branch_name>to merge
<branch name>into your current branch. I also created aliases
git merge stagingand
git merge masteras these are branches I frequently merge in.
gb <branch_name>, an alias for
git checkout. Why not
gc? Well, I already used that for
git commit, also, this alias involves working with branches, and can be grouped with the next two commands, which involve branch management. Just like with merging, I created
gbsto switch to master and staging branches, respectively.
gbn <branch_name>creates a new branch called
<branch_name>, an alias for
git checkout -b.
gblto list all branches (
clean_git_branchesis a recently new one I added. This deletes all merged branches, except for master and staging, because I always want to keep these. It is an alias for
git branch --merged | grep -v "\*" | grep -v master | grep -v staging | xargs -n 1 git branch -d. This command runs
git branch -dagainst all merged branches, excluding the current branch, the branch
masterand the branch
staging. It is a nice way to quickly clean up the list of local branches.
If you want to get started with these aliases, just copy the code below into your
alias gs="git status" alias gd="git diff" alias gl="git log --oneline" alias ga="git add -A" alias gc="git commit -m" alias gp="git push" alias gu="git pull" alias gb="git checkout" alias gbl="git branch" alias gbn="git checkout -b" alias gbm="git checkout master" alias gbs="git checkout staging" alias gm="git merge" alias gms="git merge staging" alias gmm="git merge master" alias clean_git_branches="git branch --merged | grep -v "\*" | grep -v master | grep -v staging | xargs -n 1 git branch -d"
These shortcuts have really sped up my development process. I’m interested to hear what techniques other devs use to speed up their work. How does this list of aliases look? Is there anything that could be added?