例如,在一台新的Ubuntu机器上,我刚刚运行了sudo apt-get git
,当输入例如git check[tab]
时没有自动完成。
我在http://git-scm.com/docs中没有找到任何东西,但如果我没记错的话,现在自动完成包含在git软件包中,我只需要在我的bashrc中添加正确的条目即可。
例如,在一台新的Ubuntu机器上,我刚刚运行了sudo apt-get git
,当输入例如git check[tab]
时没有自动完成。
我在http://git-scm.com/docs中没有找到任何东西,但如果我没记错的话,现在自动完成包含在git软件包中,我只需要在我的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
在某些Ubuntu版本中,默认情况下可能会出现git自动补全无法正常工作的问题,通过运行以下命令重新安装可以修复:
sudo apt-get install git-core bash-completion
sudo apt-get install git-core bash-completion
你可以使用Homebrew或者MacPorts安装git补全。
如果 $BASH_VERSION
> 4: brew install bash-completion@2
(更新版本)
请特别注意你的bash版本,因为MacOS默认版本是3.2.57(1)-release。
添加到.bash_profile
:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
对于旧版本的bash:brew install bash-completion
将其添加到.bash_profile
中:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
sudo port install git +bash_completion
然后将此添加到您的.bash_profile
中:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
在此指南中了解更多信息:安装 Bash git 自动补全
请注意,无论何种情况下,您都需要创建一个新的 shell(打开一个新的终端选项卡/窗口)才能使更改生效。
/usr/share/bash-completion/completions/git
。/etc/bash_completion.d/git-prompt
用于提供 git 提示支持,而不是补全。 - Rui Chenbrew uninstall bash-completion
然后再执行 brew install git
。以上步骤也适用于这种情况。 - patapouf_aiecho $BASH_VERSION
。 - Guglie我也遇到了同样的问题,按照以下步骤进行:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
然后将以下行添加到您的.bash_profile
中(通常位于您的主文件夹下)
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
export https_proxy=proxy_ip:proxy_port
我真的不明白为什么它不能从系统中获取设置。 - madlymad大多数指令都会告诉你下载
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
并在你的bash启动脚本中引用它,比如 .bashrc
。
但是这种方法存在问题,因为它引用的是master
分支,也就是最新版本的git-completion.bash
。问题在于有时它会因为不兼容安装的git版本而出错。
实际上,现在这种方法会失败,因为master
分支的git-completion.bash
新增了需要git v2.18的新特性,可是目前还没有哪个包管理器或安装程序升级到这个版本。你会收到一个错误信息:unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
所以最安全的解决方案是引用与你安装的git版本匹配的版本/标签。例如:
https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash
请注意URL中使用的版本号是v2.17.
而不是master
。当然,确保在bash启动脚本中引用它。/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
为了确保安全,我将其与 Github 上的 2.17.1 版本进行了比较,并且匹配。 - jmtgit-core
和 bash-completion
sudo apt-get install -y git-core bash-completion
对于当前会话使用:
source /usr/share/bash-completion/completions/git
始终在所有会话中开启它。echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
只需在您的 ~/.bashrc
文件中执行以下操作:
source /usr/share/bash-completion/completions/git
其他回答告诉你要安装 bash-completion
,但其实不需要这么做。如果你真的想安装,那么就不需要直接使用源来完成。你只需要选择其中一种方法即可,不需要两种都用。
一个更通用的解决方案是按照 bash-completion 项目建议的查询系统位置。
source "$(pkg-config --variable=completionsdir bash-completion)"/git
在我的Ubuntu系统中,有一个文件安装在这里:
source /etc/bash_completion.d/git-prompt
您可以跟随这些链接进入/usr/lib/git-core
文件夹。您可以在那里找到有关如何设置PS1或使用__git_ps1
的说明。
在所有已发布的 macOS 的答案中,此方法只被提到了一次。这是由 jmt 在一个非常简短的评论中提到的...
如果你已经安装了 Xcode 开发人员工具,那么你不需要下载任何新东西。
相反,你只需要找到已经存在的 git-completion.bash
文件并将其源化在你的.bashrc
文件中。请检查以下目录:
/Applications/Xcode.app/Contents/Developer/usr/share/git-core
/Library/Developer/CommandLineTools/usr/share/git-core
如果以上方法均无效,Git 本身也可以帮助你解决问题。当我执行以下命令时,Git 将返回一个设置,该设置来自于和我的git-completion.bash
文件位于同一目录下的gitconfig
文件:
$ git config --show-origin --list
...
file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig credential.helper=osxkeychain
...
或者您可以始终对计算机进行暴力搜索,同时喝一些咖啡:
$ find / -type f -name git-completion.bash 2>/dev/null
~/.bashrc
文件中添加了以下内容:# Git shell completion and prompt string on macOS
_git_dir="/Applications/Xcode.app/Contents/Developer/usr/share/git-core"
if [ -f "${_git_dir}/git-completion.bash" ]; then
source "${_git_dir}/git-completion.bash"
fi
if [ -f "${_git_dir}/git-prompt.sh" ]; then
source "${_git_dir}/git-prompt.sh"
fi
unset _git_dir
find / -type f -name git-completion.bash 2>/dev/null
查找文件位置,然后引用它。然后在整个 completion
目录中使用 grep
命令查找 __git_ps1()
函数的位置,并将其引用。这样我就可以通过将 $PS1
环境变量从 Git Bash 复制到 MSYS2 Bash 中来获得提示符。 - Adrianecho $SHELL
,或者查看 this question。
/etc/bash_completion.d/
或/usr/share/bash-completion/completions/git
。您可能正在寻找解决方法,因为它没有工作,这可能是因为补全脚本没有被使用。尝试将以下行添加到您的.bashrc
文件中:source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
sudo apt-get install git-core bash-completion
.zshrc
文件中添加以下内容:autoload -Uz compinit && compinit
可选:考虑添加Oh My Zsh插件,以获取带有git信息的主题。
有关更多详细信息,请参阅git书籍附录A:其他环境中的Git - 在Zsh中使用Git
在Windows上,可以通过posh-git将制表符自动完成功能添加到Powershell中。(请注意,此功能不支持命令提示符)。
要在Powershell中安装posh-git,请执行以下操作:
脚本执行策略必须设置为RemoteSigned
或Unrestricted
。通过执行Get-ExecutionPolicy
命令来检查脚本执行策略设置。如果策略没有设置为所需的两个值之一,以管理员身份运行 PowerShell 并执行Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
。PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
然后在新的终端中再次尝试制表符自动完成。
有关Chocolatey、Scoop或手动安装说明,请参阅posh-git GitHub页面。
可选:还可以考虑升级到Powershell 7,它具有用于智能制表符自动完成的命令行智能感知功能。
有关更多详细信息,请参阅git书籍附录A:Powershell中的其他环境中的Git
也许对某些人有帮助:--
从以下链接下载.git-completion.bash文件后,
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
当我尝试使用__git_ps1函数时,遇到了错误--
-bash: __git_ps1: command not found
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
然后在您的.bash_profile中添加以下内容:
source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi
source ~/.bash.git
将会执行下载的文件。
export PS1='\W$(__git_ps1 "[%s]")
命令将在当前工作目录后附加检出的分支名称(如果它是一个 git 存储库)。
因此,它会像这样:
dir_Name[branch_name]
其中 dir_Name 是工作目录名称,branch_name 是您当前正在使用的分支的名称。
请注意 - __git_ps1 区分大小写。
在 Windows 11 的命令提示符(CMD)中,以下是我最终的解决方案:
C:\Users\<username>\AppData\local\clink
目录中
(cd ~ && exec cat .bashrc | grep completion)
。 - aderchoxgrep completion ~/.bashrc
来检查它是否处于活动状态,而不需要使用exec
和 无用的 cat。 - jswetzen