Git自动补全

1 cd ~

2 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3 brew list

4 brew install bash-completion

5 brew info bash-completion

6 open .bash_profile

if [ -f ~/.git-completion.bash ]; then
    . ~/.git-completion.bash
fi

source .bash_profile


9.https://github.com/git/git/edit/master/contrib/completion/git-completion.bash

下载以上git-completion.bash至/Documents目录下,

cp /Documents/git-completion.bash ~/.git-completion.bash



http://www.oschina.net/translate/10-tips-git-next-level?cmp

### 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、付费专栏及课程。

余额充值