Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save actionjack/6755a7679ea556357c0a466272d78011 to your computer and use it in GitHub Desktop.

Select an option

Save actionjack/6755a7679ea556357c0a466272d78011 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.

@actionjack
Copy link
Copy Markdown
Author

alias g='git'
alias ga='git add'
alias gall='git add -A'
alias gap='git add -p'
alias gb='git branch'
alias gbD='git branch -D'
alias gba='git branch -a'
alias gbd='git branch -d'
alias gbm='git branch -m'
alias gbt='git branch --track'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcaa='git commit -a --amend -C HEAD'
alias gcam='git commit -v -am'
alias gcamd='git commit --amend'
alias gcb='git checkout -b'
alias gci='git commit --interactive'
alias gcl='git clone'
alias gclean='git clean -fd'
alias gcm='git commit -v -m'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcom='git checkout main'
alias gcount='git shortlog -sn'
alias gcp='git cherry-pick'
alias gcpd='git checkout main; git pull; git branch -D'
alias gcpx='git cherry-pick -x'
alias gcsam='git commit -S -am'
alias gct='git checkout --track'
alias gd='git diff'
alias gdel='git branch -D'
alias gds='git diff --staged'
alias gdv='git diff -w "$@" | vim -R -'
alias gen_readme='curl https://raw.githubusercontent.com/jehna/readme-best-practices/master/README-default.md > README.md'
alias get='git'
alias gexport='git archive --format zip --output'
alias gf='git fetch --all --prune'
alias gft='git fetch --all --prune --tags'
alias gftv='git fetch --all --prune --tags --verbose'
alias gfv='git fetch --all --prune --verbose'
alias gg='git log --graph --pretty=format:'''%C(bold)%h%Creset%C(magenta)%d%Creset %s %C(yellow)<%an> %C(cyan)(%cr)%Creset''' --abbrev-commit --date=relative'
alias ggui='git gui'
alias ghm='cd "$(git rev-parse --show-toplevel)"'
alias git='hub'
alias git-amend-author='git commit --amend --author="Martin Jacksonmartin.jackson1@digital.hmrc.gov.uk" --no-edit'
alias github='openup'
alias gl='git pull'
alias gll='git log --graph --pretty=oneline --abbrev-commit'
alias glum='git pull upstream main'
alias gm='git merge'
alias gmu='git fetch origin -v; git fetch upstream -v; git merge upstream/main'
alias gmv='git mv'
alias gnew='git log HEAD@{1}..HEAD@{0}'
alias gp='git push'
alias gpatch='git format-patch -1'
alias gpf='git push origin --force-with-lease'
alias gpo='git push origin HEAD'
alias gpom='git push origin main'
alias gpp='git pull && git push'
alias gpr='git pull --rebase'
alias gpristine='git reset --hard && git clean -dfx'
alias gprom='git fetch origin main && git rebase origin/main && git update-ref refs/heads/main origin/main'
alias gpu='git push --set-upstream'
alias gpunch='git push --force-with-lease'
alias gpuo='git push --set-upstream origin'
alias gpuoc='git push --set-upstream origin $(git symbolic-ref --short HEAD)'
alias gr='git remote'
alias gra='git remote add'
alias grm='git rm'
alias grv='git remote -v'
alias gs='git status'
alias gsl='git shortlog -sn'
alias gss='git status -s'
alias gst='git stash'
alias gstb='git stash branch'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias gstpo='git stash pop'
alias gstpu='git stash push'
alias gstpum='git stash push -m'
alias gsts='git stash push'
alias gstsm='git stash push -m'
alias gsu='git submodule update --init --recursive'
alias gsw='git switch'
alias gswc='git switch --create'
alias gswm='git switch main'
alias gswt='git switch --track'
alias gt='git tag'
alias gta='git tag -a'
alias gtd='git tag -d'
alias gtl='git tag -l'
alias gtls='git tag -l | gsort -V'
alias gu='git ls-files . --exclude-standard --others'
alias gup='git fetch && git rebase'
alias gus='git reset HEAD'
alias gwc='git whatchanged'

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