Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 79 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save JuggoPop/10706934 to your computer and use it in GitHub Desktop.
Save JuggoPop/10706934 to your computer and use it in GitHub Desktop.
Git branch bash autocomplete *with aliases* (add to .bash_profile)
# To Setup:
# 1) Save the .git-completion.bash file found here:
# https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
# 2) Add the following lines to your .bash_profile, be sure to reload (for example: source ~/.bash_profile) for the changes to take effect:
# Git branch bash completion
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
# Add git completion to aliases
__git_complete g __git_main
__git_complete gc _git_checkout
__git_complete gm __git_merge
__git_complete gp _git_pull
fi
# Make sure you actually have those aliases defined, of course.
alias g="git"
alias gc="git checkout"
alias gm="git merge"
alias gp="git pull"
@retrospectacus
Copy link

The git-completion.bash file already exists on your system; you do not need to download it into your home directory.
On this system (Ubuntu 18.10):

if [ -r /usr/share/bash-completion/completions/git ] 
then
        .  /usr/share/bash-completion/completions/git
        __git_complete gco _git_checkout
        __git_complete gd _git_diff
fi

alias gco='git checkout'
alias gs='git status'
alias gd='git diff'
alias gf='git fetch'
alias gst='git stash'
alias gsp='git stash pop'
alias gsl='git stash list'
alias gpoh='git push origin HEAD'
alias grom='git rebase origin/main'
alias gmom='git merge origin/main'
alias gfomm='git fetch origin main:main'
alias gcam='git commit -a -m'
alias gcaa='git commit -a --amend'
alias gcm='git commit -m'
alias glog="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"

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