Skip to content

Instantly share code, notes, and snippets.

@jbgo
Created December 7, 2011 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgo/1443992 to your computer and use it in GitHub Desktop.
Save jbgo/1443992 to your computer and use it in GitHub Desktop.
Bash completion for git branches that (still) sucks
_completion_list() {
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$*" -- ${cur}) )
}
_complete_git() {
if [ -d .git ]; then
branches=`git branch | cut -c 3-`
tags=`git tag`
_completion_list $branches $tags
fi
}
_complete_git_remote() {
branches=`git branch -a | grep 'remotes/' | cut -f '3' -d '/'`
_completion_list $branches
}
complete -F _complete_git git
complete -F _complete_git g
complete -F _complete_git_remote grb track
complete -F _complete_git grb

Bash completion for git branches that (still) sucks

There are a lot of git branch completion scripts floating around out there. Here's another one.

Why?

  1. It's stupid simple (unlike some other scripts)
  2. It supports any possible git command that uses a branch (or tag) name
  3. It works when git is aliased to g and/or you are using git aliases (and you should be)
  4. It only completes remote branches when using grb track (gem install git-remote-branch)
  5. It's available to me when I inevitably work on other computers.

Installation

curl https://raw.github.com/gist/1443992/200d077154a79f8b0ab32980ecb2e48b1372b8c7/.git-completion.bash > ~/.git-completion.bash
echo 'source ~/.git-completion.bash' >> ~/.bashrc
source ~/.bashrc

Caveats

Adds branch completion to all git commands, not just branch-specific commands. This isn't an issue for me, and it probably won't be for you either.

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