更新:

更新:

我不确定 git 补全功能在哪里实现,但我在 MacOS 上运行 zsh 和 oh-my-zsh。如果我输入,git branch -v我会看到我有 4 个本地分支

> git branch -v
  dangling-pointer       ce141cd55 Metal: Fix dangling pointer in IntermRebuild
* fix-occlusion-query-m1 67a147feb Metal: Fix Occlusion Query bug on M1
  main                   93c0c90db Suppress the failing egl test to unblock roller
  readpixel-speed-test   3ebef5614 Metal:Read Pixels Speed Test

但如果我输入git checkout <tab>我得到这个

❯ git checkout <tab>
zsh: do you wish to see all 6036 possibilities (776 lines)? 

其他 6032 种可能性是标签和一些其他相关内容,例如HEAD,,FETCH_HEADORIG_HEAD

检查另一个仓库,只有一个分支

❯ git branch -v
* master 8ce048a build

但自动完成显示

❯ git checkout <tab>
zsh: do you wish to see all 186 possibilities (34 lines)? 

如果我选择是

❯ git checkout
FETCH_HEAD                                   fix-uniform-block-padding-issue              origin/dev                                   test                                       
HEAD                                         lgtm-test                                    origin/fix-uniform-block-padding-issue       uniform-tree                               
ORIG_HEAD                                    master                                       origin/lgtm-test                             v4.21.2                                    
allow-sharing-arrays                         origin/HEAD                                  origin/master                                v4.22.0                                    
cap-sphere                                   origin/allow-sharing-arrays                  origin/test                                  v4.22.1                                    
dev                                          origin/cap-sphere                            origin/uniform-tree                                                                     
v0.0.1   v0.0.9   v0.0.17  v0.0.25  v0.0.34  v0.0.42  v1.5.1   v2.1.0   v2.6.1   v3.0.2   v3.4.1   v4.0.1   v4.5.1   v4.9.1   v4.11.2  v4.14.0  v4.17.0  v4.19.5                
v0.0.2   v0.0.10  v0.0.18  v0.0.26  v0.0.35  v1.0.0   v1.5.2   v2.1.1   v2.6.2   v3.1.0   v3.5.0   v4.1.0   v4.5.2   v4.9.2   v4.11.3  v4.14.1  v4.18.0  v4.20.0                
v0.0.3   v0.0.11  v0.0.19  v0.0.27  v0.0.36  v1.1.0   v1.6.0   v2.1.3   v2.7.0   v3.2.0   v3.6.0   v4.2.0   v4.6.0   v4.9.3   v4.11.4  v4.14.2  v4.18.1  v4.21.0                
v0.0.4   v0.0.12  v0.0.20  v0.0.29  v0.0.37  v1.2.0   v1.6.1   v2.2.0   v2.8.0   v3.2.1   v3.7.0   v4.3.0   v4.7.0   v4.9.4   v4.11.5  v4.15.0  v4.19.0  v4.21.1                
v0.0.5   v0.0.13  v0.0.21  v0.0.30  v0.0.38  v1.3.0   v1.7.0   v2.3.0   v2.8.1   v3.2.2   v3.7.1   v4.3.1   v4.8.0   v4.10.0  v4.12.0  v4.15.1  v4.19.1  v4.21.2                
v0.0.6   v0.0.14  v0.0.22  v0.0.31  v0.0.39  v1.4.0   v1.7.1   v2.4.0   v2.8.2   v3.3.0   v3.8.0   v4.3.2   v4.8.1   v4.10.1  v4.12.1  v4.15.2  v4.19.2  v4.22.0                
v0.0.7   v0.0.15  v0.0.23  v0.0.32  v0.0.40  v1.4.1   v1.8.0   v2.5.0   v3.0.0   v3.3.1   v3.8.1   v4.4.0   v4.8.2   v4.11.0  v4.13.0  v4.16.0  v4.19.3  v4.22.1                
v0.0.8   v0.0.16  v0.0.24  v0.0.33  v0.0.41  v1.5.0   v1.9.0   v2.6.0   v3.0.1   v3.4.0   v4.0.0   v4.5.0   v4.9.0   v4.11.1  v4.13.1  v4.16.3  v4.19.4                       
8ce048a  -- [HEAD]    build (4 days ago)
5a4e7b5  -- [HEAD^]   bump to v4.22.1 (4 days ago)
5c85f3a  -- [HEAD^^]  changelist (4 days ago)
9a19a3f  -- [HEAD~3]  fixed missing return types in `vertex-arrays.js` (4 days ago)
05807c5  -- [HEAD~4]  build (8 weeks ago)
b35e44a  -- [HEAD~5]  bump to v4.22.0 (8 weeks ago)
297e0d1  -- [HEAD~6]  add samples to AttachmentOptions (8 weeks ago)
aa7fe4a  -- [HEAD~7]  build (8 months ago)
a3cb143  -- [HEAD~8]  update docs (8 months ago)
7944f37  -- [HEAD~9]  build (8 months ago)
9850f58  -- [HEAD~10] typo (8 months ago)
bf3b5c1  -- [HEAD~11] build (8 months ago)
74eb006  -- [HEAD~12] typo (8 months ago)
859ebd0  -- [HEAD~13] build (8 months ago)
8718e95  -- [HEAD~14] typo (8 months ago)
11d1c92  -- [HEAD~15] remove unused modules (8 months ago)
f8141e9  -- [HEAD~16] move old tests to new test harness (8 months ago)
b8a7b1a  -- [HEAD~17] build (8 months ago)
190c144  -- [HEAD~18] bump to v4.21.2 (8 months ago)
2a3aeff  -- [HEAD~19] fix for Safari's lack of lookbehind assertions (8 months ago)

我不知道为什么它显示最后 20 次提交(或最后的提交)

有没有办法配置 git 自动完成功能,使其仅显示本地分支?因为这样,自动完成功能几乎没用为我除非我已经知道分支名称,以便我可以输入几个字符并过滤完成的内容。

更新:

有人建议--no-guess。这给出了一个较小的列表,但还不够小。

❯ git branch -v
  backup-packpixels                           923bd8756 Metal:Speed up BGRA8 to RGBA8 copy for readPixels
* buffersubdata-perf-test                     8b2807fc0 Metal: Use Vulkan style BufferSubData impl
  check-cb-order                              7e92ef8aa Metal: Fix for not submitting command buffers in order
  clear-surface                               924f6de7d Metal:Clear Backbuffer when Robust Resource Init enabled
  main                                        29d18b303 [behind 39] Revert "Vulkan: Use VK_EXT_primitive_topology_list_restart"

> git checkout <tab>
zsh: do you wish to see all 6486 possibilities (2176 lines)? 
> git checkout --no-guess
zsh: do you wish to see all 3298 possibilities (1113 lines)? 

但条目数仍然太多,git branch -v只有 4 个分支

❯ export GIT_COMPLETION_CHECKOUT_NO_GUESS=1
❯ git checkout 
zsh: do you wish to see all 6450 possibilities (6450 lines)? 

没有效果

虽然我看到它被引用~/.oh-my-zsh/plugins/gitfast/git-completion.bash

也没有

__gitcomp_nl "$(__git_refs...

git-completion.bash文件中。

答案1

帖子 在 Git Bash 中禁用远程分支的自动完成? 有几个建议。

这是一个 erik.weathers 的回答

我假设你正在使用git-completion.bash脚本,并且你仅有的关心git checkout

_git_checkout ()为了实现这一点,我只需更改函数定义中的一行 git-completion.bash

<         __gitcomp_nl "$(__git_refs '' $track)"
---
>         __gitcomp_nl "$(__git_heads '' $track)"

我的理解是,这只会影响制表符完成操作(因为它位于*switch-case 语句的 case 中)。

其他 Toland Hon 的回答 做了更多:

根据上述 erik.weathers 的回答,我做了以下更改,以便自动完成功能可以根据当前前缀同时适用于本地和远程。默认情况下,它只会搜索本地,但如果我指定, origin/…它会知道我也想搜索远程分支。

_git_checkout ()方法中,更改

__gitcomp_nl "$(__git_refs '' $track)"

到:

# only search local branches instead of remote branches if origin isn't specified
if [[ $cur == "origin/"* ]]; then
    __gitcomp_nl "$(__git_refs '' $track)"
else
    __gitcomp_nl "$(__git_heads '' $track)"
fi

当然,您可以更改origin为其他内容,或者如果有多个,您可以让它通过远程前缀列表进行搜索。

已接受 VonC 的回答 方法是使用:

git checkout --no-guess ...
# or:
export GIT_COMPLETION_CHECKOUT_NO_GUESS=1

答案2

简短而甜蜜的回答是,是的,有。

这篇博客文章提供了有关如何实现此目标的易于遵循的说明:https://cmetcalfe.ca/blog/git-checkout-autocomplete-local-branches-only.html

笔记:在文章中,Carey 建议创建一个新的别名来检查本地分支,即结帐

我强烈建议采用这种方法,因为有时您可能需要查看完整的选项列表 - 因此分离命令是理想的。

,如果你使用 brew 在 MacOS 上安装 git,那么你要查找的配置文件可能在这里:

$(brew --prefix)/etc/bash_completion.d/git-completion.bash

如果您没有按照此信息操作,请告诉我,我会进行更深入的探讨。

相关内容