例如,在一台新的ubuntu机器上,我刚刚运行了sudo apt-get git,在输入例如git check[tab]时没有完成。
我在http://git-scm.com/docs上没有找到任何东西,但IIRC完成是包含在这些天的git包中,我只需要在我的bashrc中正确的条目。
例如,在一台新的ubuntu机器上,我刚刚运行了sudo apt-get git,在输入例如git check[tab]时没有完成。
我在http://git-scm.com/docs上没有找到任何东西,但IIRC完成是包含在这些天的git包中,我只需要在我的bashrc中正确的条目。
当前回答
参见https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
您只需要获取完成脚本的源代码
其他回答
macOS通过Xcode开发工具
在目前发布的所有macOS的答案中,这只在jmt的一个非常简短的评论中提到…
如果你已经安装了Xcode开发工具,那么你不需要下载任何新工具。
相反,您只需要定位已经存在的git-completion。Bash文件并在.bashrc中获取它。检查以下目录:
/应用程序/ xcode /内容/开发/ usr / share / git-core /图书馆/开发/ CommandLineTools / usr / share / git-core
如果做不到这一点,git本身可能能够帮助您。当我像下面这样运行git配置时,git报告了一个来自gitconfig文件的设置,该文件与我的git-completion.bash位于同一目录:
$ 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
注意,这也是git提示字符串脚本的来源,因为它位于同一个目录中。
(在macOS Catalina中测试)
Mac M1
对于那些使用Mac M1环境的用户,我可以通过自制软件进行安装:
酿造安装bash-completion
然后添加到我的~/。Bash_profile或~/。Bashrc(不管你用什么):
[[ -r "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh"
您可能需要更新版本号(1.3_3)。你只需要在那个目录中查找即可。我很想知道有没有更好的办法。
可能对某人有帮助:——
下载完。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
显然,我们需要从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
然后在.bash_profile中添加以下内容
source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi
~ / .bash来源。Git会执行下载的文件
export PS1='\W$(__git_ps1 "[%s]")命令将在当前工作目录(如果它是git存储库)之后追加签出分支名称。
所以它看起来像:-
dir_Name[branch_name],其中dir_Name是工作目录名称,branch_name将是您当前工作的分支名称。
请注意——__git_ps1是区分大小写的。
Ubuntu
这里有一个美丽的答案。在Ubuntu 16.04上为我工作
窗户
Git Bash是允许自动完成的工具。不确定这是否是标准发行版的一部分,所以你可以发现这个链接也很有用。 顺便说一下,Git Bash允许使用Linux shell命令在windows上工作,这对于有GNU/Linux环境经验的人来说是一件很棒的事情。
只需在.bashrc中输入下面的内容,然后重新启动终端。导航到Git repo以查看提示符中的路径。
PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] @ \[\033[0;36m\]\h \w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] '