Skip to content

Instantly share code, notes, and snippets.

@punyaruchal
Created May 25, 2021 12:46
Show Gist options
  • Save punyaruchal/8043d0830bf0d792dc08d764b61bee4f to your computer and use it in GitHub Desktop.
Save punyaruchal/8043d0830bf0d792dc08d764b61bee4f to your computer and use it in GitHub Desktop.
Git Bash Aliases and Functions

Git Command-Line Shortcuts

A lot of my time is spent in Terminal and a majority of it is spent typing Git commands. I created a set of keyboard shortcuts with Bash aliases and functions to speed up my workflow and save me hundreds of keystrokes every 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)

Notes: If you’ve never set an alias before, don’t know where to put them, or have no clue what I’m talking about, read my post on Terminal/Bash Command-Line Shortcuts with Aliases before continuing.

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"; }

You can quickly see how these aliases can save you keystrokes. Most of the aliases are pretty straight forward—for example, instead of git add assets/css/screen.css, you can run:

ga assets/css/screen.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment