先日間違えてgit push -fしてしまい、激しく後悔したのでこれを機会にgitの設定を見直すことにしました。
前提
- homebrewがインストールされている
0. ローカルのgitのバージョンを確認する
git --versionでまず現在のgitを確認すると
git version 1.7.10.2 (Apple Git-33)
かなり古いバージョンを使っている&そもそもhomebrewのgitですらないっぽいことが分かりました…
せっかくなので、ここでhomebrewに切り替えようと考えました。
1. homebrewのgitを最新化
brew updateした後にbrew upgrade gitでhomebrewのgitを最新化します。
2. エラーが出る…
brew upgrade gitしたときに下記のエラーがでます。
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link git'
で、試しにbrew link gitするとまたエラー。
brew link git
Linking /usr/local/Cellar/git/1.8.2.3... Warning: Could not link git. Unlinking...
Error: Could not symlink file: /usr/local/Cellar/git/1.8.2.3/etc/bash_completion.d/git-completion.bash
Target /usr/local/etc/bash_completion.d/git-completion.bash already exists. You may need to delete it.
To force the link and overwrite all other conflicting files, do:
brew link --overwrite formula_name
To list all files that would be deleted:
brew link --overwrite --dry-run formula_name
brew link --overwrite --dry-run formula_nameとのことなのでbrew link --overwrite --dry-run gitをしてみると
brew link --overwrite --dry-run git
Would remove:
/usr/local/bin/git-upload-pack
/usr/local/bin/git-upload-archive
/usr/local/bin/git-shell
/usr/local/bin/git-receive-pack
/usr/local/bin/git-cvsserver
/usr/local/bin/git
で、問題なさそうな気がしたのでおもむろにbrew link --overwrite gitを実行。
※よくわかっていないのでそのまま実行してトラブルがあっても責任はとれません…
3. やっとできたと思ってgit --versionを実行したら古いまま
git --version
git version 1.7.10.2 (Apple Git-33)
4. pathが通っていないせいで古いgitを参照してしまうため、/etc/pathsを書き換える
他にもいろいろ方法はあるみたいですが、自分はとりあえず/etc/pathsを書き換える方法を選択しました。
sudo vi /etc/pathsで下記のように編集。
cat /etc/paths
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
5. ちゃんとhomebrewのgitを参照していることを確認
そのままだと変更されないのでターミナル再起動or. ~/.zshrcしてからバージョンを確認。
git --version
git version 1.8.2.3
以上で完了です。
最後に
git push時に表示されるwarning: push.default is unset...の意味と解決方法 #git - Qiita [キータ]
この設定をしておくと間違ってすべてのrepositoryをforce pushしてしまう危険がなくなるのでおすすめです。これをやるためにバージョンアップしたのでした。