How not to display tags in git autocompletion?

93 Views Asked by At

When using completion in git, for example on the git checkout command, all tags are returned. I have more and more tags, which makes reading quite unpleasant.

user:~/repo$ git branch
  prod
  staging
  master
  user_branch1
  user_branch2
user:~/repo$ git checkout user_
  user_remote_branch3
  user_branch1
  user_branch1-2023-06-19T1042-6386b7b427
  user_branch1-2023-06-19T1555-cf61155208
  user_branch1-2023-07-19T1818-808a0fa125
  user_branch1-2023-07-19T1839-8c379cf567
  user_branch1-2023-07-24T1125-4b50cd52a2
  user_branch2
  user_branch2-2023-06-21T1457-c323d6697a
  user_old_branch4-2023-06-21T1818-25006b3d62
  user_old_branch5-2023-03-02T1512-63fa5377f7

I truncated my example but currently for 3 active branches, I have more than 30 results

The --no-guess option doesn't remove the tags.
It is not possible to delete these tags on the remote for history purposes.

It seems to me that a possible solution could be :

  • no longer retrieve tags for each git pull/git fetch (I don't know how to automate it without having to add --no-tags on each command)

  • delete existing ones

But it looks fragile. All it takes is one oversight to have to delete all the tags again.
And I might have to use these tags one day.

Based on reading Disable auto-completion of remote branches in Git Bash?

I see it's possible to edit git's behavior by editing the file.
\usr\share\bash-completion\completions\git

I have confirmed its presence but its understanding remains a mystery to me. Is there a way to limit auto-completion to branches only?

Thanks

1

There are 1 best solutions below

5
On

Provided you're using the official bash completion script for Git, adding this into your ~/.bashrc should produce the desired effect of git checkout tab-completion not showing or completing tags:

_git_checkout() {
    __git_complete_refs --dwim --mode=heads "$@"
}

Then, of course, re-open your terminal emulator, or re-login over SSH, or you can simply run exec bash -l.