【问题标题】:How to configure git bash command line completion?如何配置 git bash 命令行补全?
【发布时间】:2023-03-11 01:50:01
【问题描述】:

例如在一台新的 ubuntu 机器上,我刚刚运行了sudo apt-get git,并且在键入时没有完成,例如git check[tab].

我在http://git-scm.com/docs 上没有找到任何东西,但是这些天在 git 包中包含了 IIRC 完成,我只需要在我的 bashrc 中输入正确的条目。

【问题讨论】:

  • 在 Ubuntu Precise(和 Fedora 17)上为我开箱即用。
  • 要检查你是否默认拥有它,你可以运行(cd ~ && exec cat .bashrc | grep completion)

标签: git bash


【解决方案1】:

在 Linux 上

在大多数发行版上,安装 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

在 Mac 上

您可以使用 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

Mac 端口

sudo port install git +bash_completion

然后将其添加到您的.bash_profile:

if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
fi

本指南中的更多信息:Install Bash git completion

请注意,在所有情况下,您都需要创建一个新的 shell(打开一个新的终端选项卡/窗口)才能使更改生效。

【讨论】:

  • 大多数 *nix 机器(尤其是 Ubuntu)已经拥有该文件,因此只需将其提供给我的用户即可解决我的问题。谢谢。
  • ubuntu 可能有它作为 /etc/bash_completion.d/git-prompt
  • 我有 Git 但没有 /etc/bash_completion.d
  • 在我的 Ubuntu 14.04 中,这个文件是 /usr/share/bash-completion/completions/git/etc/bash_completion.d/git-prompt 用于 git prompt 支持,而不是用于完成。
  • 警告:Mac homebrew 方法仅在您通过 homebrew 安装 git 时才有效,如果您之前通过其他方法安装了 git,则 brew uninstall bash-completion 然后brew install git,那么上述步骤将起作用。
【解决方案2】:

我遇到了同样的问题,请按照以下步骤操作:

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

来源:http://code-worrier.com/blog/autocomplete-git/

【讨论】:

  • 我在代理后面,所以我必须先设置代理才能让 curl 工作export https_proxy=proxy_ip:proxy_port 我真的不明白为什么它没有从系统获取设置。
  • 实际上不需要在文件名前用一个点来隐藏该文件。另外:小心使用 git 版本(参见 wisbucky 的答案)
  • 在 Linux 上按照上述说明操作后,我需要退出终端并再次打开以查看结果。另外,我在 curl Url 中更改了 git 版本。
【解决方案3】:

您看到的大多数说明都会告诉您下载

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 启动脚本中获取它。

【讨论】:

  • 这是最好的 macOS 答案。
  • 哇...经过大约一个小时的 git 抨击我的大脑之后,您的评论是关键。感谢您提供此解决方案!黄金!
  • 特定于 macos 版本的文件应该已经在磁盘上:/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash 为了安全起见,我将它与 github 上的 2.17.1 版本进行了比较,它匹配。
  • 在 Linux 上按照上述说明操作后,我需要退出终端并再次打开以查看结果。
【解决方案4】:

Ubuntu 14.10

安装git-corebash-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
    

【讨论】:

  • 谢谢,这在 Ubuntu 16.04 LTS 上对我有用 :)
  • 在 Debian 10 WSL 中最适合我。我没有在其他答案中提到的/etc/bash_completion.d/ 中的完成脚本,也不想从 github 手动下载一些东西
【解决方案5】:
【解决方案6】:

只需在您的 ~/.bashrc 中执行此操作:

source /usr/share/bash-completion/completions/git

其他答案告诉您安装bash-completion,您不需要这样做,但如果您这样做,则无需直接获取完成。你做一个或另一个,而不是两者兼而有之。

更通用的解决方案是按照 bash-completion 项目的建议查询系统位置:

source "$(pkg-config --variable=completionsdir bash-completion)"/git

【讨论】:

  • 您的通用解决方案在 Ubuntu 18.04 上非常适合我。首先,您的方法非常简洁——谢谢!
  • 通用解决方案在 Void Linux 上运行良好,但我确实必须先安装 bash-completion。
  • bash-completion 应该自动完成。如果您有 git bash-completion 但没有 bash-completion,则需要它。
【解决方案7】:

在我的 ubuntu 上,这里安装了一个文件:

source /etc/bash_completion.d/git-prompt

您可以通过链接进入/usr/lib/git-core 文件夹。你可以在那里找到一个说明,如何设置 PS1 或使用__git_ps1

【讨论】:

    【解决方案8】:

    可能对某人有帮助:--

    从以下链接下载 .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
    

    source ~/.bash.git 将执行下载的文件和

    export PS1='\W$(__git_ps1 "[%s]") 命令将在当前工作目录(如果它是 git 存储库)之后附加结帐分支名称。

    所以它看起来像:-

    dir_Name[branch_name] 其中 dir_Name 是工作目录名称,branch_name 是您当前正在处理的分支的名称。

    请注意——__git_ps1 区分大小写。

    【讨论】:

      【解决方案9】:

      Arch Linux

      在 bash 启动文件之一中获取 /usr/share/git/completion/git-completion.bash

      例如:

      # ~/.bashrc
      
      source /usr/share/git/completion/git-completion.bash
      

      您也许可以在 /usr/share/bash-completion/completions/git 等其他位置找到该脚本,但这些脚本对我不起作用。

      【讨论】:

        【解决方案10】:

        窗口

        它最终如何在 Windows 10 命令行 (cmd) 上为我工作:

        【讨论】:

          【解决方案11】:

          Ubuntu

          有一个漂亮的答案here。在 Ubuntu 16.04 上为我工作

          窗口

          Git Bash 是允许自动完成的工具。不确定这是否是标准分发的一部分,因此您可以找到 this link 也很有用。 顺便说一句,Git Bash 允许使用 Linux shell 命令 在 windows 上工作,这对于有 GNU/Linux 环境经验的人来说是一件好事。

          【讨论】:

            【解决方案12】:

            Mac M1

            对于那些使用 Mac M1 环境的人,我可以通过 homebrew 安装:

            brew install 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)。您只需要在该目录中查找它。我很想知道是否有更好的方法。

            【讨论】:

            • 应该是[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh" - 安装程序建议这样做。
            【解决方案13】:

            macOS 通过 Xcode 开发者工具

            在目前针对 macOS 发布的所有答案中,这仅在 jmt 的一个非常简短的 comment 中提到...

            如果您已经安装了 Xcode 开发者工具,那么您不需要下载任何新的东西。

            相反,您只需找到已经存在的git-completion.bash 文件并将其源到您的.bashrc 中。检查以下目录:

            • /Applications/Xcode.app/Contents/Developer/usr/share/git-core
            • /Library/Developer/CommandLineTools/usr/share/git-core

            如果做不到这一点,git 本身也许可以帮助你。当我按如下方式运行git config 时,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 prompt-string 脚本的来源,因为它位于同一目录中。

            (在 macOS Catalina 中测试)

            【讨论】:

              【解决方案14】:

              在 Git 项目的 Github 上,他们提供了一个 bash 文件来自动完成 git 命令。

              您应该将它下载到主目录并强制 bash 运行它。这只是两个步骤,并在以下博客文章中完美解释(一步一步)。

              code-worrier blog: autocomplete-git/

              我已经在 mac 上测试过了,它应该也可以在其他系统上运行。您可以将相同的方法应用于其他操作系统。

              【讨论】:

                猜你喜欢
                • 2017-05-09
                • 1970-01-01
                • 1970-01-01
                • 2016-05-13
                • 2013-06-22
                • 1970-01-01
                • 2012-07-13
                相关资源
                最近更新 更多