Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List only local branches when autocompleting git checkout (Zsh)
git config --global alias.checkoutr checkout
$EDITOR /usr/local/share/zsh/site-functions/git-completion.bash

...and then modify the file as follows...

-__gitcomp_nl "$(__git_refs '' $track)"
+if [ "$command" = "checkoutr" ]; then
+    __gitcomp_nl "$(__git_refs '' $track)"
+else
+    __gitcomp_nl "$(__git_heads '' $track)"
+fi

... and source your shell (source ~/.bashrc / source ~/.zshrc...).

Use git checkoutr for the default behavior and git checkout (or just gco) for browsing only local branches. Credits to http://cmetcalfe.ca/blog/git-checkout-autocomplete-local-branches-only.html.

@mrinal-scio
Copy link

mrinal-scio commented Dec 10, 2020

To get this working on zsh, you might need to get https://github.com/git/git/blob/master/contrib/completion/git-completion.zsh, follow the instructions in the comments to setup a link to git-completion.bash, then edit the latter file according to the instructions above

Note that the $track variable has now been renamed to $dwim

@luoluoluolin
Copy link

luoluoluolin commented Feb 25, 2021

For new version of git(2.30.1):

  1. modify file /usr/local/share/zsh/site-functions/git-completion.bash: copy shell function _git_checkout to _git_checkoutr, then change _git_checkout function below:
if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
-			__git_complete_refs --mode="refs"
+			__git_complete_refs --mode="heads"
		elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
			__git_complete_refs --mode="remote-heads"
		else
-			__git_complete_refs $dwim_opt --mode="refs"
+			__git_complete_refs $dwim_opt --mode="heads"
		fi
  1. add GIT_COMPLETION_CHECKOUT_NO_GUESS=1 to .zshrc/.bash_profile
  2. git config --global alias.checkoutr checkout

@anthonycvella
Copy link

anthonycvella commented Apr 7, 2022

@luoluoluolin Any ideas how to get this to work for git 2.35? I am running in zsh, made these changes, and it is still pulling remote branches in autocomplete.

@Nick-foote
Copy link

Nick-foote commented May 18, 2022

@luoluoluolin Any ideas how to get this to work for git 2.35? I am running in zsh, made these changes, and it is still pulling remote branches in autocomplete.

I've just followed the steps above and confirmed it works with git version 2.35.1

@KarelCemus
Copy link

KarelCemus commented May 29, 2022

@anthonycvella you must have enabled plugin gitfast otherwise it doesn't work. It took me a while to figure it out.

@anthonycvella
Copy link

anthonycvella commented May 31, 2022

@KarelCemus Hmm I don't have this plugin enabled.

I have the following enabled: plugins=(git keychain gpg-agent)

@KarelCemus
Copy link

KarelCemus commented May 31, 2022

@anthonycvella I didn't have it enabled either. My point is when I did enable gitfast it magically started to work; although there is not much magic, I've read the scripts so IMO this plugin must be enabled in order to have this working. Can you try to enable gitfast and check if this gist works for you?

@ekoneko
Copy link

ekoneko commented Oct 20, 2022

@luoluoluolin It not works for me in dwim mode. I think $dwim_opt is not necessary if only want to get local branches?

                else
-                       __git_complete_refs $dwim_opt --mode="refs"
+                       __git_complete_refs --mode="heads"
                fi

or run

git config --global --type=bool checkout.guess false

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