Git Command-Line Shortcuts

As a programmer cum consultant who have a lot of hand-on on programming, I actually spend a lot of time on typing Git commands. I actually found a set of keyboards shortcuts with Bash aliases and it actually speed up my Git workflow as well as save me hundreds of keystrokes every single day.

Git Bash Aliases and Functions

Git allows you to set aliases but they’re limited and only save you a few keystrokes (i.e. instead of git checkout you can type git co, but you still have to type git). Since Bash is Terminal’s default command-line interpreter, you can also set Bash aliases to reduce your keystrokes even further.

Here’s my list of Git Bash aliases and functions. To use them as your own, just add them to the file you store your aliases/functions. (i.e. ~/.bash_profile or ~/.bashrc)

When copy & pasting, it’s important to keep the spacing. (i.e. for aliases, there must be no spaces before and after the equal signs, and for functions, there must be a space after the opening curly bracket of the declaration and a semicolon after the command. Don’t forget to reload your file (source ~/.bash_profile) or restart Terminal after making changes.

# ----------------------
# Git Aliases
# ----------------------
alias ga='git add'
alias gaa='git add .'
alias gaaa='git add --all'
alias gau='git add --update'
alias gb='git branch'
alias gbd='git branch --delete '
alias gc='git commit'
alias gcm='git commit --message'
alias gcf='git commit --fixup'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcom='git checkout master'
alias gcos='git checkout staging'
alias gcod='git checkout develop'
alias gd='git diff'
alias gda='git diff HEAD'
alias gi='git init'
alias glg='git log --graph --oneline --decorate --all'
alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
alias gm='git merge --no-ff'
alias gma='git merge --abort'
alias gmc='git merge --continue'
alias gp='git pull'
alias gpr='git pull --rebase'
alias gr='git rebase'
alias gs='git status'
alias gss='git status --short'
alias gst='git stash'
alias gsta='git stash apply'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias gsts='git stash save'

# ----------------------
# Git Functions
# ----------------------
# Git log find by commit message
function glf() { git log --all --grep="$1"; }

This is same of the sample how to use the shortcut for the aliases to save your keystrokes. Maybe for the beginning, may need to keep refer back to the list above but after sometime, you should be able to easily remember they key because all the aliases are pretty straight forward.

For example: instead of git add public/html/css.css

ga public/html/css.css

or instead of git checkout -b <branch-name>:

gcob <branch-name>

There are a few that are a but more custom. I use a couple of variations of git log that I find more useful. gld is a detailed, one-line view of git log:

$ gld
a12f7c3 2021-06-14 Merge branch 'feature-1'
d24f827 2021-06-14 fix merge conflicts
bf7d83d 2021-06-14 fix typo
76c807a 2021-06-14 fix document
c7fc3d0 2021-06-14 commit master
de8fa98 2021-06-07 merge feature-4 conflict
12f714f 2021-06-07 done feature 4, can merge to master
6cdde0f 2021-06-03 start on feature 3
b1b4a80 2021-06-03 start on feature 4
a30be1b 2021-06-03 start feature 2
20f5919 2021-06-03 Merge branch 'feature-3'
feeb031 2021-06-03 Complete feature 3
1d2fa6d 2021-06-03 Merge branch 'hotfix-1'
b486102 2021-06-03 completed feature 2
2aff0b7 2021-06-03 start on feature 4
82a0407 2021-06-03 add another few line in feature 2
6a02e6d 2021-06-03 start feature 2
ac8e089 2021-06-03 added new function

while glg is a detailed, decorative graph view of git log:

By default gm, the alias for git merge, defaults to --no-ff (no fast-forward)—many times I have more than 1 feature branch in development, each of those branches has more than 1 commit, and I feel that the history of the merges are important.

Aliases are great but are limited in scope, which is why I had to use a function for glf, my Git log find function. Occasionally I want to search for a commit by the commit message. Using glf:

glf "commit message"
david:david-test$ glf
commit a12f7c31d64e60febd3c711b6a755f31bc627a09 (HEAD -> master)
Merge: d24f827 bf7d83d
Author: David Cheong <[email protected]>
Date:   Mon Jun 14 16:49:49 2021 +0800

    Merge branch 'feature-1'

commit d24f82760312cfb98a60a4f4c3953e5569fadbdd
Author: David Cheong <[email protected]>
Date:   Mon Jun 14 15:38:09 2021 +0800

    fix merge conflicts

commit bf7d83da9e5eb126eff13640c7ab729a37301700 (feature-1)     
Author: David Cheong <[email protected]>
Date:   Mon Jun 14 15:23:16 2021 +0800

    fix typo

There are more aliases that you can add by your own base on your personal preferences, the above is some of the sample that I will be use from day to day, for sure, there are some of my daily use aliases not added into the list, and I type it in manually. Overall, the list already save a lot of keystrokes that I use everyday.

Leave a Reply

google.com, pub-3772983857049267, DIRECT, f08c47fec0942fa0
%d bloggers like this: