如何配置 git bash 命令行完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12399002/
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
How to configure git bash command line completion?
提问by Vincent Scheib
E.g. on a fresh ubuntu machine, I've just run sudo apt-get git
, and there's no completion when typing e.g. git check[tab]
.
例如,在新的 ubuntu 机器上,我刚刚运行了sudo apt-get git
,并且在输入 eg 时没有完成git check[tab]
。
I didn't find anything on http://git-scm.com/docs, but IIRC completion is included in the git package these days and I just need the right entry in my bashrc.
我在http://git-scm.com/docs上没有找到任何东西,但是现在 git 包中包含了 IIRC 完成,我只需要在我的 bashrc 中输入正确的条目。
回答by Sergey Evstifeev
On Linux
在 Linux 上
on most distributions, git completion script is installed into /etc/bash_completion.d/
(or /usr/share/bash-completion/completions/git
) when you install git, no need to go to github. You just need to use it - add this line to your .bashrc
:
在大多数发行版中,当您安装 git 时,git 完成脚本已安装到/etc/bash_completion.d/
(或/usr/share/bash-completion/completions/git
) 中,无需转到 github。您只需要使用它 - 将此行添加到您的.bashrc
:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
In some versions of Ubuntu, git autocomplete may be broken by default, reinstalling by running this command should fix it:
在某些版本的 Ubuntu 中,默认情况下 git autocomplete 可能已损坏,通过运行此命令重新安装应该可以修复它:
sudo apt-get install git-core bash-completion
On Mac
在 Mac 上
You can install git completion using Homebrew or MacPorts.
您可以使用 Homebrew 或 MacPorts 安装 git completion。
Homebrew
家酿
if $BASH_VERSION
> 4: brew install bash-completion@2
(updated version)
Pay special care which version of bash you have as MacOS default ships with 3.2.57(1)-release.
if $BASH_VERSION
> 4:(brew install bash-completion@2
更新版本)请特别注意您拥有的 bash 版本,因为 MacOS 默认随 3.2.57(1)-release 一起提供。
add to .bash_profile
:
添加到.bash_profile
:
if [ -f /usr/local/share/bash-completion/bash_completion ]; then
. /usr/local/share/bash-completion/bash_completion
fi
For older versions of bash: brew install bash-completion
对于旧版本的 bash: brew install bash-completion
add to .bash_profile
:
添加到.bash_profile
:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
MacPorts
端口
sudo port install git +bash_completion
sudo port install git +bash_completion
then add this to your .bash_profile
:
然后将此添加到您的.bash_profile
:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
more info in this guide: Install Bash git completion
本指南中的更多信息:安装 Bash git 完成
Note that in all cases you need to create a new shell (open a new terminal tab/window) for changes to take effect.
请注意,在所有情况下,您都需要创建一个新的 shell(打开一个新的终端选项卡/窗口)才能使更改生效。
回答by jospratik
i had same issue, followed below steps:
我有同样的问题,请按照以下步骤操作:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
then add the following lines to your .bash_profile
(generally under your home folder)
然后将以下行添加到您的.bash_profile
(通常在您的主文件夹下)
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
回答by wisbucky
Most of the instructions you see will tell you to download
您看到的大部分说明都会告诉您下载
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
and source that in your bash startup script like .bashrc
.
并在您的 bash 启动脚本中提供它,如.bashrc
.
But there is a problem with that, because it is referencing the master
branch, which is the latest version of git-completion.bash
. The problem is that sometimes it will break because it is not compatible with the version of git you've installed.
但是有一个问题,因为它引用了master
分支,这是git-completion.bash
. 问题是有时它会损坏,因为它与您安装的 git 版本不兼容。
In fact, right now that will break because the master
branch's git-completion.bash
has new features that requires git v2.18, which none of the package managers and installers have updated to yet. You'll get an error unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
实际上,现在这将中断,因为master
分支的git-completion.bash
新功能需要 git v2.18,而包管理器和安装程序尚未更新到该功能。你会得到一个错误unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
So the safest solution is to reference the version/tag that matches the git you've installed. For example:
因此,最安全的解决方案是引用与您安装的 git 匹配的版本/标签。例如:
https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash
https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash
Note that it has a v2.17.
in the URL instead of master
. And then, of course, make sure to source that in the bash startup script.
请注意,它v2.17.
在 URL 中有一个而不是master
。然后,当然,请确保在 bash 启动脚本中找到它的来源。
回答by Andrzej Rehmann
Ubuntu 14.10
Ubuntu 14.10
Install git-core
and bash-completion
安装git-core
和bash-completion
sudo apt-get install -y git-core bash-completion
For current session usage
source /usr/share/bash-completion/completions/git
To have it always on for all sessions
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
对于当前会话使用
source /usr/share/bash-completion/completions/git
让它在所有会话中始终开启
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
回答by wich
See https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
见https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
You just need to source the completion script
您只需要获取完成脚本
回答by kisp
on my ubuntu there is a file installed here:
在我的 ubuntu 上,这里安装了一个文件:
source /etc/bash_completion.d/git-prompt
you can follow the links into the /usr/lib/git-core
folder. You can find there an instruction, how to set up PS1 or use __git_ps1
您可以按照链接进入/usr/lib/git-core
文件夹。您可以在那里找到说明,如何设置 PS1 或使用__git_ps1
回答by Nicks
May be helpful for someone:--
可能对某人有帮助:--
After downloading the .git-completion.bash from the following link,
从以下链接下载 .git-completion.bash 后,
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
and trying to use __git_ps1 function, I was getting error as--
并尝试使用 __git_ps1 函数,我收到错误 -
-bash: __git_ps1: command not found
Apparently we need to download scripts separately from master to make this command work, as __git_ps1 is defined in git-prompt.sh . So similar to downloading .git-completion.bash , get the git-prompt.sh:
显然我们需要从 master 单独下载脚本才能使这个命令工作,因为 __git_ps1 在 git-prompt.sh 中定义。与下载 .git-completion.bash 类似,获取 git-prompt.sh:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
and then add the following in your .bash_profile
然后在您的 .bash_profile 中添加以下内容
source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi
source ~/.bash.git will execute the downloaded file and
source ~/.bash.git 将执行下载的文件并
export PS1='\W$(__git_ps1 "[%s]")
command will append the checkout out branch name after the current working directory(if its a git repository).
export PS1='\W$(__git_ps1 "[%s]")
命令将在当前工作目录(如果它是一个 git 存储库)之后附加检出分支名称。
So it will look like:-
所以它看起来像:-
dir_Name[branch_name]
where dir_Name is the working directory name and branch_name will be the name of the branch you are currently working on.
dir_Name[branch_name]
其中 dir_Name 是工作目录名称, branch_name 将是您当前正在处理的分支的名称。
Please note -- __git_ps1 is case sensitive.
请注意——__git_ps1 区分大小写。
回答by FelipeC
Just do this in your ~/.bashrc
:
只需在您的~/.bashrc
:
source /usr/share/bash-completion/completions/git
Other answers are telling you to install bash-completion
, you don't need to do that, but if you do, then there's no need to source the completion directly. You do one or the other, not both.
其他答案告诉您 install bash-completion
,您不需要这样做,但如果您这样做,则无需直接获取完成。你做一个或另一个,而不是两个。
A more generic solution is querying the system location as recommended by the bash-completion project:
一个更通用的解决方案是按照 bash-completion 项目的建议查询系统位置:
source "$(pkg-config --variable=completionsdir bash-completion)"/git
回答by Rob
Arch Linux
拱形Linux
Source /usr/share/git/completion/git-completion.bash
in one of the bash startup files.
/usr/share/git/completion/git-completion.bash
bash 启动文件之一中的源。
For example:
例如:
# ~/.bashrc
source /usr/share/git/completion/git-completion.bash
You may be able to find the script in other locations like /usr/share/bash-completion/completions/git
but these scripts did not work for me.
您也许可以在其他位置找到该脚本,/usr/share/bash-completion/completions/git
但这些脚本对我不起作用。
回答by andrgolubev
Ubuntu
Ubuntu
There is a beautiful answer here. Worked for me on Ubuntu 16.04
有一个美丽的答案在这里。在 Ubuntu 16.04 上对我来说有效
Windows
视窗
Git Bashis the tool to allow auto-completion. Not sure if this is a part of standard distribution so you can find this linkalso useful. By the way, Git Bash allows to use Linux shell commandsto work on windows, which is a great thing for people, who have experience in GNU/Linux environment.
Git Bash是允许自动完成的工具。不确定这是否是标准发行版的一部分,因此您会发现此链接也很有用。顺便说一句,Git Bash 允许使用Linux shell 命令在 Windows 上工作,这对于有 GNU/Linux 环境经验的人来说是一件好事。