例如,我会输入:
git checkout task[TAB]
由此导致控制台经常停顿数分钟。是否有一种方法仅限于本地分支来限制自动完成?
从 Git 2.13 (2017 年第二季度) 开始,您可以禁用(部分)分支自动补全功能。
git checkout --no-guess ...
# or:
export GIT_COMPLETION_CHECKOUT_NO_GUESS=1
peff
)完成。gitster
--合并于提交b439747, 2017年5月1日)
如现在所记录在contrib/completion/git-completion.bash
中:
注意:DWIM是 Do What I Mean 的缩写, 它指的是系统尝试预测用户想要做什么,自动纠正微小错误,而不是盲目执行用户明确但可能不正确的输入。You can set the following environment variables to influence the behavior of the completion routines:
GIT_COMPLETION_CHECKOUT_NO_GUESS
When set to "1", do not include "DWIM" suggestions in
git-checkout
completion (e.g., completing "foo" when "origin/foo" exists).
completion
: 可选禁用 DWIM 检出当我们为 "
git checkout
" 补全分支名称时,我们还会补全可能触发 DWIM 行为的远程分支名称。根据您的工作流和项目,这可能既方便又烦人。例如,我的
gitster.git
克隆包含 74 个本地 "jk/*
" 分支,但 origin 包含另外 147 个。
当我想要检出一个本地分支但无法完全记住名称时,制表符补全会显示 251 个条目。更糟糕的是,对于已被选中用于 pu 的主题,上游分支名称很可能与我的类似,导致我选择错误并意外创建新分支的概率很高。
This patch adds a way for the user to tell the completion code not to include DWIM suggestions for checkout.
This can already be done by typing:git checkout --no-guess jk/<TAB>
but that's rather cumbersome.
The downside, of course, is that you no longer get completion support when you do want to invoke the DWIM behavior.
But depending on your workflow, that may not be a big loss (for instance, in git.git I am much more likely to want to detach, so I'd type "git checkout origin/jk/<TAB>
" anyway).
git switch
时设置GIT_COMPLETION_CHECKOUT_NO_GUESS=1
。@VonC - lajarre我假设你正在使用git-completion.bash
脚本,并且你只关心git checkout
。
为了实现这一点,我只在git-completion.bash
中_git_checkout ()
函数的定义中更改了一行:
< __gitcomp_nl "$(__git_refs '' $track)"
---
> __gitcomp_nl "$(__git_heads '' $track)"
我的理解是,这只影响了选项补全操作(因为它位于 switch-case 语句的 *
案例内部)。
sudoedit /usr/share/bash-completion/completions/git
,然后更改 @erik.weathers 回答中提到的那一行,重新打开 bash,完成!非常感谢。 - mschaynarefs
行为。__git_checkout()现在只用于heads
。我创建了一个别名rco = checkout。这样,如果你想要旧的行为,你仍然可以得到它。 - Matt Hughes"")
使用__git_heads
,但*)
使用__git_refs
。这样它只建议头部,但如果您已经输入了某些内容(即t
),它将给出任何匹配的引用(即tag/sometag
)。如果这仍然太多,它也可以匹配tag/
-> tag/sometags
等。 - arcyqwerty/usr/local/etc/bash_completion.d/git-completion.bash
。 - Toland Hon/usr/local/etc/bash_completion.d/git-completion.bash
按照上面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
更改为其他内容,或者如果有多个远程前缀,则可以让它搜索远程前缀列表。你可以黑掉 /etc/bash_completion.d/git
你需要编辑 __git_refs ()
请注意,此更改的行为将在任何地方应用(因此即使在使用 git push/pull 时,您可能不希望它这样做)。 当然,你可以复制该函数或传递额外的参数,但我将其留给你决定。
refs="refs/tags refs/heads refs/remotes"
更改为只有refs="refs/heads"
,并注释掉整个“尝试查找与完成单词匹配的远程分支”的部分。这样做不太美观,但可以正常工作 :-) - Manuela Huttergit add
众所周知非常慢) - sehegit for-each-ref | wc -l
返回11000... - Manuela Hutterco
来切换本地分支,而使用完整命令checkout
来切换所有分支。
你可以执行以下操作,在你的.bashrc文件中重新定义_git_checkout()
函数。你让这个函数保持不变,除了结尾部分。if [ $command -eq "co" ]; then
__gitcomp "$(__git_refs_local '' $track)"
else
__gitcomp "$(__git_refs '' $track)"
fi
__git_refs_local
,在其中删除远程内容即可。if [ $command = "co" ]; then
。 - pR0Ps$(brew --prefix)/etc/bash_completion.d/git-completion.bash
并不是一个好主意,因为每次通过 Homebrew 更新 Git 时都会被覆盖。.bash_profile
中的 _git_checkout
函数。_git_checkout ()
{
__git_has_doubledash && return
case "$cur" in
--conflict=*)
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
;;
--*)
__gitcomp "
--quiet --ours --theirs --track --no-track --merge
--conflict= --orphan --patch
"
;;
*)
# check if --track, --no-track, or --no-guess was specified
# if so, disable DWIM mode
local flags="--track --no-track --no-guess" track=1
if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
track=''
fi
# 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
;;
esac
}
Carey Metcalfe写了一篇博客文章,提供了一个解决方案,还编辑了自动完成功能,但使用的代码略微更新于其他答案。他还定义了一个别名checkoutr
,以保留旧的自动完成行为,以防需要。
简而言之,首先使用此命令创建checkoutr
别名:
git config --global alias.checkoutr checkout
git-completion.bash
,将 _git_checkout
函数复制到您的 shell 的 RC 文件中以便重新定义它,并在该函数内部替换此行:__git_complete_refs $track_opt
使用以下代码行:
if [ "$command" = "checkoutr" ]; then
__git_complete_refs $track_opt
else
__gitcomp_direct "$(__git_heads "" "$cur" " ")"
fi
请查看博客文章了解更多细节和代码的可能更新。
我自己没有使用Git Bash,但如果这与http://tekrat.com/2008/04/30/bash-autocompletion-git-super-lazy-goodness/中提到的相同,那么你应该能够用普通的git branch替换git branch -a
_complete_git() {
if [ -d .git ]; then
branches=`git branch -a | cut -c 3-`
tags=`git tag`
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${branches} ${tags}" -- ${cur}) )
fi
}
complete -F _complete_git git checkout
__git_refs()
似乎可以解决问题。 - Manuela HutterFWW 这里有一个 hack 来完成 __git_complete_refs 的技巧
__git_complete_refs ()
{ 本地远端轨道 pfx cur_="$cur" sfx=" "
while test $# != 0; do
case "$1" in
--remote=*) remote="${1##--remote=}" ;;
--track) track="yes" ;;
--pfx=*) pfx="${1##--pfx=}" ;;
--cur=*) cur_="${1##--cur=}" ;;
--sfx=*) sfx="${1##--sfx=}" ;;
*) return 1 ;;
esac
shift
done
echo cur_ $cur_ > a
if [[ $GIT_COMPLETION_CHECKOUT_NO_GUESS != 1 || $cur_ == "origin"* ]]; then
__gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
else
__gitcomp_direct "$(__git_heads "" "$cur_")"
fi
}
git checkout --no-guess ...
或export GIT_COMPLETION_CHECKOUT_NO_GUESS=1
:这两者都会禁用分支补全。请参见我的答案。 - VonC