GIT 自动补全

照网上的其他人写的安装遇到一些问题,最后按下面这个地址中的步骤安装OK
https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion

其他参考:
https://stackoverflow.com/questions/12399002/how-to-configure-git-bash-command-line-completion
https://blog.youkuaiyun.com/chenbifeng/article/details/51570606
https://github.com/yszheda/wiki/wiki/git-tips#bash_autocomplete_Unknown_option_listcmdslistmainporcelainothersnohelpersaliaslistcompleteconfig

这里只记录下Mac下的,其他系统请直接看上面链接。

  1. Homebrew 应该都有的的吧

  2. brew install git bash-completion .
    安装git 和 bash-completion ,已安装跳过。
    (Install Git and bash-completion: brew install git bash-completion (Note: If this install fails with a 404 error, and you already have git installed, just remove the git part of this brew install))

  3. Add bash-completion to your ~/.bash_profile:
    将以下语句追加到~/.bash_profile文件中(没有则创建)

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion || {
    # if not found in /usr/local/etc, try the brew --prefix location
    [ -f "$(brew --prefix)/etc/bash_completion.d/git-completion.bash" ] && \
        . $(brew --prefix)/etc/bash_completion.d/git-completion.bash
}
  1. source ~/.bash_profile

感觉要注意不同系统,不同系统版本(mac的bash版本),不同git版本 ,安装和配置方式可能都有区别。遇到问题google 、baidu吧,我就遇到按tab 提示的时候出现Unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config

### Git 自动补全分支名称功能的配置方法 #### Linux 系统下的配置 在 Linux 系统中,可以通过将 `git-completion.bash` 文件放置到特定路径来启用自动补全功能。具体操作如下: 1. 将 `git-completion.bash` 文件复制到 `/etc/bash_completion.d/` 目录下[^2]。 如果该文件不存在,则可以按照以下方式获取: ```bash git clone https://github.com/git/git.git cp git/contrib/completion/git-completion.bash /etc/bash_completion.d/ ``` 2. 加载脚本并验证是否生效: ```bash source /etc/bash_completion.d/git-completion.bash ``` 此时,在终端输入 `git checkout de<Tab>` 后应能自动补全为 `dev` 或其他匹配的分支名称。 --- #### macOS (Bash) 系统下的配置 对于使用 Bash 的 macOS 用户,同样需要加载 `git-completion.bash` 文件。以下是具体步骤: 1. 获取 `git-completion.bash` 文件: ```bash git clone https://github.com/git/git.git cp git/contrib/completion/git-completion.bash ~/.git-completion.bash ``` 2. 修改 `.bash_profile` 文件以加载此脚本: ```bash echo 'source ~/.git-completion.bash' >> ~/.bash_profile source ~/.bash_profile ``` 3. 测试自动补全功能是否正常工作。 如果通过 Homebrew 安装了 Git,则可以直接利用其内置的完成支持。 --- #### macOS (Zsh) 系统下的配置 许多 macOS 用户默认使用 Zsh 作为 shell,因此需要额外调整才能使 Git 自动补全起作用。以下是具体的设置过程: 1. 克隆官方 Git 存储库中的补全脚本: ```bash git clone https://github.com/git/git.git mkdir ~/.zsh cp git/contrib/completion/git-completion.zsh _git mv _git ~/.zsh/ ``` 2. 编辑用户的 `.zshrc` 文件以引入这些更改: ```bash echo 'fpath=(~/.zsh $fpath)' >> ~/.zshrc echo 'autoload -U compinit && compinit' >> ~/.zshrc source ~/.zshrc ``` 此时重新打开终端窗口即可测试效果[^4]。 --- #### Windows 系统下的配置 在 Windows 上实现类似的体验主要依赖于 Cygwin 或 MinGW 工具链环境。下面是基于 Git for Windows 的解决方案: 1. 找到安装目录内的 `git-completion.bash` 文件位置(通常是 `%GIT_HOME%\mingw64\share\git\completion`),将其链接至个人配置区域; 2. 对于集成开发工具如 VSCode ,则需编辑 settings.json 添加相关选项[^5]: ```json { "terminal.integrated.profiles.windows": { "Git-Bash": { "path": "C:\\Program Files\\Git\\bin\\bash.exe", "args": [] } }, "terminal.integrated.defaultProfile.windows": "Git-Bash" } ``` 完成后重启应用程序确认改动已应用成功。 --- ```python # 示例 Python 脚本用于模拟简单的字符串匹配逻辑 def auto_complete(input_str, candidates): return [cand for cand in candidates if input_str in cand] branches = ['main', 'develop', 'feature-x', 'hotfix-y'] print(auto_complete('de', branches)) # 输出可能的结果列表 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值