Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
@JanCVanB
Copy link

JanCVanB commented Sep 17, 2015

All of the common commands (the ones I alias) have only one leading underscore.

@danielcompton
Copy link

danielcompton commented Sep 27, 2015

Thanks for this!

@Aginskij
Copy link

Aginskij commented Jan 20, 2016

Thank you!

@manleyhimself
Copy link

manleyhimself commented Jun 28, 2016

Nice! Thanks!

@tucq88
Copy link

tucq88 commented Jul 20, 2016

Nice and clean. Tested & worked with Ubuntu 14.04

@vedhavyas
Copy link

vedhavyas commented Oct 20, 2016

worked :)

@zero-t4
Copy link

zero-t4 commented May 11, 2017

how can i set alias like this pld = !git pull origin develop?

@tplk
Copy link

tplk commented May 29, 2017

@zero-t4, add alias pld="!git pull origin develop" to your .bash_profile (or wherever you store your aliases).

@davich
Copy link

davich commented Jun 7, 2017

Awesome! Thanks

@bcuz
Copy link

bcuz commented Nov 22, 2017

Line 13 should be:

__git_complete gm _git_merge

One too many _ in the last part of it.

@loogle18
Copy link

loogle18 commented Dec 20, 2017

Thanks!

@wowkalucky
Copy link

wowkalucky commented Feb 15, 2018

Yay!! Awesomenessss...

@joeydong-stripe
Copy link

joeydong-stripe commented Mar 1, 2018

👍

@noelworden
Copy link

noelworden commented Mar 16, 2018

Thank you!

@AndersonZacharyT
Copy link

AndersonZacharyT commented Apr 4, 2018

The legend

@jnothman
Copy link

jnothman commented Aug 30, 2018

I've got a much longer list of aliases... Perhaps this helper is useful:

gitshort() {
	short=$1; shift
	long=$1; shift
	alias $short="git $long $@"
	__git_complete $short _git_$long
}

then

gitshort gc checkout
gitshort gcb checkout -b
gitshort gm merge
gitshort gp pull

@raineorshine
Copy link

raineorshine commented Mar 22, 2020

Another option: You can source this script after your aliases are created to automatically enable autocompletion for all of them.

Like so:

curl https://gist.githubusercontent.com/raineorshine/17df90a6f80a19adc8103e12a2b43eca/raw/fc1a7f7f39fb5edb59f34850dcbca8b912b3bb34/alias_completion.sh usr/local/etc/alias_completion
echo "source /usr/local/etc/alias_completion" >> ~/.bash_profile
source ~/.bash_profile

@retrospectacus
Copy link

retrospectacus commented Mar 7, 2022

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