Skip to content

Instantly share code, notes, and snippets.

@mmrko
Last active July 20, 2023 08:48
Show Gist options
  • Select an option

  • Save mmrko/b3ec6da9bea172cdb6bd83bdf95ee817 to your computer and use it in GitHub Desktop.

Select an option

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.

@elcortez

elcortez commented Feb 8, 2018

Copy link
Copy Markdown

You just saved me from hundreds of tiny frustrations :)

@keenahn

keenahn commented Jan 4, 2019

Copy link
Copy Markdown

@aok1425 @mmrko thanks a million. Got here through Google.

@joelhock

Copy link
Copy Markdown

yeah, this is great. i had to do additional machinery to store the original $command to get checkoutr to work; i think the git repo's source might necessitate that now.

@alswl

alswl commented Mar 18, 2019

Copy link
Copy Markdown

/usr/local/share/zsh/site-functions/git-completion.bash works for me.

macOS, zsh in brew + oh-my-zsh


update in M1

/opt/homebrew/share/zsh/site-functions/git-completion.bash

@zihotki

zihotki commented Mar 25, 2019

Copy link
Copy Markdown

I'm encountering one issue, I can't make checkoutr working. For some reason when any alias is used for checkout the $command parameter is always checkout but not checkoutr or any other alias. I'm using bash on Windows. Did anybody encounter it? To test I'm using the same snippet with echos:

        if [ "$command" = "rcheckout" ]; then
            echo "rcheckout!"
            __git_complete_refs $track_opt            
        else
            echo "checkout!"
            __gitcomp_nl "$(__git_heads '' $track)"
        fi

@CodeRedDev

Copy link
Copy Markdown

@zihotki I just encountered the same issue. I'm just using normal Mac bash aliases and my solution was just to create a copy of _git_checkout that will be called from my remote checkout alias.

@OmisNomis

Copy link
Copy Markdown

I've added the amended _git_checkout () function in my .zshrc file, but the function doesn't work unless I manually source for every new terminal session.

I assume this is because the git plugin is loaded after the redeclaration, is there a way around this? Currently, I've amended the git-completion.bash file manually.

@gkop

gkop commented Aug 31, 2020

Copy link
Copy Markdown

In hopes that it could save another person some time debugging, I just wanted to say I found that I needed to install git from homebrew, and then the autocompletion worked perfectly out of the box with macos Mojave and oh-my-zsh. The system git that comes with developer tools had otherwise worked well for me, I never realized that I was even using it versus git from brew. But for the autocompletion of local branches only to work, I found I needed git from brew.

@mrinal-scio

mrinal-scio commented Dec 10, 2020

Copy link
Copy Markdown

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

luoluoluolin commented Feb 25, 2021

Copy link
Copy Markdown

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
Copy Markdown

@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
Copy Markdown

@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
Copy Markdown

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

@anthonycvella

Copy link
Copy Markdown

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

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

@KarelCemus

Copy link
Copy Markdown

@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

ekoneko commented Oct 20, 2022

Copy link
Copy Markdown

@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

gbidkar commented Jan 31, 2023

Copy link
Copy Markdown

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

That does the trick, thanks!

@anthonycvella

anthonycvella commented Feb 24, 2023

Copy link
Copy Markdown

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