Skip to content

Instantly share code, notes, and snippets.

@mmrko
Last active July 20, 2023 08:48
Show Gist options
  • Save mmrko/b3ec6da9bea172cdb6bd83bdf95ee817 to your computer and use it in GitHub Desktop.
Save mmrko/b3ec6da9bea172cdb6bd83bdf95ee817 to your computer and use it in GitHub Desktop.
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.

@anthonycvella
Copy link

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

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

@KarelCemus
Copy link

@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

@gbidkar
Copy link

gbidkar commented Jan 31, 2023

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

That does the trick, thanks!

@anthonycvella
Copy link

anthonycvella commented Feb 24, 2023

I finally figured this out for anyone wondering. I had to change the implementation defined within the gitfast plugin directory.

For me this was located here:
~/.oh-my-zsh/plugins/gitfast/git-completion.bash

Changes that work

The suggestion by @ekoneko to remove $dwim_opt

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

Changes that do not work

Note you do NOT need to set checkout.guess = false in your .gitconfig. I found by doing so, it actually broke the ability to use git checkout for local search and git checkoutr for remote search.

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