LoginSignup
4
3

More than 5 years have passed since last update.

git-completion.bash利用時にエラーが出るため、HomebrewでのGitを利用するようにする

Last updated at Posted at 2018-09-24

対象バージョン

  • Mac OS Hight Sierra 10.13.6(17G65)
  • git version 2.17.1 (Apple Git-112)
  • git version 2.19.0
  • bash-completion stable 1.3

現象

https://git-scm.com/doc
のPro Gitで紹介されている、git-completion.bashの設定をし、

bash
$ curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/git-completion.bash
$ echo source '~/git-completion.bash' >> ~/.bash_profile
$ source ~/.bash_profile

git brでタブを押し、補完を利用しようとした所、下記エラーが発生する

bash
$ git brunknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

原因

curlで落としてきたgit-completion.bashに紐付いているgitのバージョンと
Command Line Toolsに紐付いているgitのバージョンが合っておらず、
2.18から追加されたlist-cmdsがgit-completion.bashに存在せずにエラーになっている

$ xcode-select -p
/Applications/Xcode-10.app/Contents/Developer
$ git version
git version 2.17.1 (Apple Git-112)

対応

Version 2.17.1用のgit-completion.bashを利用することも考えたが、
Command Line Toolsを切り替えるごとに紐付いているgitのバージョンが変わるのは安定性に掛けるため、
Homebrewでインストールしたgitを優先して利用できるようにする。

事前準備

現象で設定したgit completionの設定を削除する

bash
$ sed -i "" "/source ~\/git-completion.bash/d" ~/.bash_profile
$ rm ~/git-completion.bash

Gitのインストール

HomebrewでGitをインストールする

bash
$ brew install git

そのままだとCommand Line Toolsに紐づくgitが利用されるため、パスの設定を行う。

$ echo export PATH='/usr/local/Cellar/git/2.14.1/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile
$ git version
git version 2.19.0

bash-completionのインストール

HomebrewでインストールしたGitに紐づくgit-completion.bashを利用するため、
bash-completionをインストールする。

$ brew install bash-completion

パス設定を行い、補完が効くようにする。

$ echo '[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion' >> ~/.bash_profile
$ source ~/.bash_profile

確認

git brでタブを押す

$ git branch

変換された

参考

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3