windows 在 Git Bash 中禁用远程分支的自动完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6623649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable auto-completion of remote branches in Git Bash?
提问by Manuela Hutter
I'm working on a fairly large git repo with a couple of thousand (remote) branches. I am used to using auto-completion (using [TAB]) in the console (Git Bash in that case), so I unconsciously do that for git commands, too.
我正在处理一个相当大的 git repo,有几千个(远程)分支。我习惯于在控制台中使用自动补全(使用 [TAB])(在这种情况下是 Git Bash),所以我也不知不觉地对 git 命令这样做了。
e.g. I'd type
例如我会输入
git checkout task[TAB]
with the effect that the console stalls for often minutes. Is there a way to limit auto-completion to local branches only?
结果是控制台停顿了几分钟。有没有办法将自动完成限制为仅本地分支?
采纳答案by VonC
With Git 2.13 (Q2 2017), you can disable (some of) the branch completion.
使用 Git 2.13(2017 年第二季度),您可以禁用(部分)分支完成。
git checkout --no-guess ...
# or:
export GIT_COMPLETION_CHECKOUT_NO_GUESS=1
See commit 60e71bb(21 Apr 2017) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
--in commit b439747, 01 May 2017)
请参阅Jeff King ( ) 的commit 60e71bb(2017 年 4 月 21 日)。(由Junio C Hamano合并-- --在提交 b439747 中,2017 年 5 月 1 日)peff
gitster
As documented in contrib/completion/git-completion.bash
now:
正如contrib/completion/git-completion.bash
现在所记录的:
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).
您可以设置以下环境变量来影响完成例程的行为:
GIT_COMPLETION_CHECKOUT_NO_GUESS
当设置为“1”时,不要在
git-checkout
完成中包含“DWIM”建议(例如,当“origin/foo”存在时完成“foo”)。
Note: DWIM is short for Do What IMean, where a system attempts to anticipate what users intend to do, correcting trivial errors automatically rather than blindly executing users' explicit but potentially incorrect inputs.
注:DWIM是短期的dØ W¯¯帽子我中号EAN,其中系统尝试预测哪些用户打算做的,纠正错误琐碎自动而不是盲目地执行用户的明确,但可能不正确的输入。
completion
: optionally disable checkout DWIMWhen we complete branch names for "
git checkout
", we also complete remote branch names that could trigger the DWIM behavior. Depending on your workflow and project, this can be either convenient or annoying.For instance, my clone of
gitster.git
contains 74 local "jk/*
" branches, but origin contains another 147.
When I want to checkout a local branch but can't quite remember the name, tab completion shows me 251 entries. And worse, for a topic that has been picked up for pu, the upstream branch name is likely to be similar to mine, leading to a high probability that I pick the wrong one and accidentally create a new branch.
completion
: 可选择禁用结帐 DWIM当我们完成“
git checkout
”的分支名称时,我们还完成了可能触发 DWIM 行为的远程分支名称。根据您的工作流程和项目,这可能很方便,也可能很烦人。例如,我的克隆
gitster.git
包含 74 个本地“jk/*
”分支,但 origin 包含另外 147 个。
当我想签出本地分支但不太记得名称时,选项卡完成显示我 251 个条目。更糟糕的是,对于一个已经被pu挑出来的topic,上游分支的名字很可能和我的很像,导致我很可能选错了一个,不小心创建了一个新的分支。
Note: "picked up for pu": see a What's cooking in git.git
: it starts with:
注意:“picked up for pu”:参见什么正在烹饪git.git
:它以:
Commits prefixed with '
-
' are only in 'pu
' (proposed updates)while commits prefixed with '+
' are in 'next
'.
以“
-
”为前缀的提交仅在“pu
”(建议更新)中,而以“+
”为前缀的提交在“next
”中。
This is part of the Git Workflow Graduation process.
pu
(proposed updates) is an integration branch for things that are not quite ready for inclusion yet
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 dowant 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).
此补丁添加了一种方法,让用户告诉完成代码不要包含用于结帐的 DWIM 建议。
这已经可以通过键入来完成:git checkout --no-guess jk/<TAB>
但这比较麻烦。
当然,缺点是当您确实想要调用 DWIM 行为时,您不再获得完成支持。
但是根据您的工作流程,这可能不是一个大损失(例如,在 git.git 中,我更有可能想要分离,所以git checkout origin/jk/<TAB>
无论如何我都会输入“ ”)。
回答by erik.weathers
I'm assuming that you are using the git-completion.bash
script, and that you onlycare about git checkout
.
我假设您正在使用该git-completion.bash
脚本,并且您只关心git checkout
.
To accomplish this, I just changed one line in the definition of the _git_checkout ()
function in git-completion.bash
:
为了实现这一点,我只是在_git_checkout ()
函数定义中更改了一行git-completion.bash
:
< __gitcomp_nl "$(__git_refs '' $track)"
---
> __gitcomp_nl "$(__git_heads '' $track)"
My understanding is that this only affects the tab-completion action (because of its location within the *
case of the switch-case statement).
我的理解是,这只影响制表符完成操作(因为它在*
switch-case 语句的case 中的位置)。
回答by sehe
You can hack /etc/bash_completion.d/git
你可以破解 /etc/bash_completion.d/git
You'll need to edit __git_refs ()
你需要编辑 __git_refs ()
Note that the change in behaviour will apply every where (so even with git push/pull where you might not want it to). You could of course, make a copy of the function or pass an extra parameter, but I leave that to you
请注意,行为的变化将适用于每个地方(因此即使使用 git push/pull 您可能不希望它发生的地方)。你当然可以复制函数或传递一个额外的参数,但我把这留给你
回答by Toland Hon
If you installed git-completion via homebrew, it's located here:
/usr/local/etc/bash_completion.d/git-completion.bash
如果您通过自制软件安装了 git-completion,它位于此处:
/usr/local/etc/bash_completion.d/git-completion.bash
Following erik.weathers' answerabove, I made the following change so autocompletion can work for both local and remote based on the current prefix. By default, it'll only search local, but if I specify origin/…
it'll know I want to search remote branches too.
按照上面erik.weathers 的回答,我进行了以下更改,以便自动完成可以根据当前前缀对本地和远程工作。默认情况下,它只会搜索本地,但如果我指定origin/…
它会知道我也想搜索远程分支。
In the _git_checkout ()
method, change
在_git_checkout ()
方法中,改变
__gitcomp_nl "$(__git_refs '' $track)"
to:
到:
# 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
Of course, you can change origin
to something else or you can have it search through through a list of remote prefixes if you have more than 1.
当然,您可以更改origin
为其他内容,或者如果您有 1 个以上的远程前缀,则可以通过远程前缀列表进行搜索。
回答by Jér?me
You could think that you just the local branches with the alias co
and all the branches with the complete command checkout
.
你可以认为你只是带有别名的本地分支co
和带有完整命令的所有分支checkout
。
You could perform the following. In your .bashrc, you redefine the _git_checkout()
function. You let this function unchanged, except the end:
您可以执行以下操作。在您的 .bashrc 中,您重新定义了该_git_checkout()
函数。你让这个函数不变,除了结尾:
if [ $command -eq "co" ]; then
__gitcomp "$(__git_refs_local '' $track)"
else
__gitcomp "$(__git_refs '' $track)"
fi
Then, you just have to define a new function, __git_refs_local
, where you remove the remote stuff.
然后,您只需要定义一个新函数__git_refs_local
,在其中删除远程内容。
回答by Rory O'Kane
Carey Metcalfe wrote a blog postcontaining a solution that also edits the auto-completion function, but with slightly newer code than other answers. He also defines an alias checkoutr
that keeps the old auto-complete behavior in case it's ever needed.
Carey Metcalfe 写了一篇博客文章,其中包含一个解决方案,该解决方案还可以编辑自动完成功能,但代码比其他答案稍新。他还定义了一个别名checkoutr
,以在需要时保留旧的自动完成行为。
In short, first create the checkoutr
alias with this command:
简而言之,首先checkoutr
使用以下命令创建别名:
git config --global alias.checkoutr checkout
Then find git-completion.bash
, copy the _git_checkout
function into your shell's RC file so that it gets redefined, and inside that function, replace this line:
然后 find git-completion.bash
,将该_git_checkout
函数复制到您的 shell 的 RC 文件中,以便重新定义它,并在该函数中替换以下行:
__git_complete_refs $track_opt
with the following lines:
有以下几行:
if [ "$command" = "checkoutr" ]; then
__git_complete_refs $track_opt
else
__gitcomp_direct "$(__git_heads "" "$cur" " ")"
fi
See the blog postfor more details and potential updates to the code.
有关更多详细信息和代码的潜在更新,请参阅博客文章。
回答by Juliusz Gonera
Modifying $(brew --prefix)/etc/bash_completion.d/git-completion.bash
is not a good idea because it will be overwritten every time you update Git through Homebrew.
修改$(brew --prefix)/etc/bash_completion.d/git-completion.bash
不是一个好主意,因为每次通过 Homebrew 更新 Git 时它都会被覆盖。
Combining all the answers I overwrite only _git_checkout
function from the completion file in my .bash_profile
after sourcing the completion file:
结合所有答案_git_checkout
,我.bash_profile
在获取完成文件后只覆盖完成文件中的功能:
_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
}
回答by Frank Schmitt
I'm not using Git Bash myself, but if this is the same as mentioned in http://tekrat.com/2008/04/30/bash-autocompletion-git-super-lazy-goodness/, you should be able to replace git branch -awith a plain git branchin
我自己没有使用 Git Bash,但如果这与http://tekrat.com/2008/04/30/bash-autocompletion-git-super-lazy-goodness/ 中提到的相同 ,你应该能够替代的Git分支-a与普通的Git分支中
_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
(in your .profile or similar) and get what you want.
(在您的 .profile 或类似文件中)并获得您想要的。
回答by SzS
FWW here is a hack to __git_complete_refs that does the trick
FWW 这里是对 __git_complete_refs 的一个黑客攻击
__git_complete_refs ()
{ local remote track pfx cur_="$cur" sfx=" "
{ 本地远程轨道 pfx cur_="$cur" sfx=""
while test $# != 0; do
case "" 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
}
}