MacにHomebrewからgitをインストールする方法

  • Macにgitをインストールしたい
  • MacのgitをHomebrewで管理したい

Macには標準でgitが存在していますが、せっかくなので、Homebrewを使って管理したいと思います。

なので、今回はMacにHomebrewからgitをインストールして、使用するgitを切り替える方法を説明します。

実際の実行手順を順を追って説明していきますので、この通りにやれば誰でもMacにHomebrewからgitをインストールすることができると思います。

この記事でわかること
  • Homebrewからgitをインストールする方法
  • Macで使用するgitを切り替える方法
目次

gitの前にHomebrewをインストール

gitをインストールするんですが、なにはともあれHomebrewがインストールされていないと話が進まないので、Homebrewをインストールします。

Homebrewのインストール方法については過去に書いたのでこちらを参照ください。

Homebrewからgitをインストール手順

標準のgitの確認

まずはHomebrewでインストールを行う前に、標準でインストールされているgitの確認を行います。

以下のコマンドでgitのバージョンを表示します。

$ git --version
git version 2.21.1 (Apple Git-122.3)

バージョンが表示されたら標準でGitがインストールされています。

Homebrewのgitの存在確認

次にHomebrewでgitが存在しているかどうかの確認です。

Homebrewのlistコマンドでbrewからインストールされているパッケージが表示されます。

$ brew list
nodebrew

[git]が表示されなければHomebrewでgitはインストールはされていません。
Homebrewで何もインストールしていない場合は、何も表示されません。

HomebrewでGitをインストール

以下のコマンドでインストールを行います。

$ brew install git

実際に実行してみるとこんな感じでログが表示されると思います。

$ brew install git
==> Installing dependencies for git: gettext and pcre2
==> Installing git dependency: gettext
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.20.1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/10/107d7f386fbeea6979f9376cdbbcf3f60943caaad61bdc754d3019ce625dffe6?__gda__=exp=1582004909~hmac=b4411838413ab537d05cb6d2edfa44a8d7e1446f7be814a46f14125a1fb6
######################################################################## 100.0%
==> Pouring gettext-0.20.1.catalina.bottle.tar.gz
==> Caveats
gettext is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD gettext library & some software gets confused if both are in the library path.

If you need to have gettext first in your PATH run:
  echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.bash_profile

For compilers to find gettext you may need to set:
  export LDFLAGS="-L/usr/local/opt/gettext/lib"
  export CPPFLAGS="-I/usr/local/opt/gettext/include"

==> Summary
🍺  /usr/local/Cellar/gettext/0.20.1: 1,893 files, 18.4MB
==> Installing git dependency: pcre2
==> Downloading https://homebrew.bintray.com/bottles/pcre2-10.34.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/af/af3bf030a455daf0a560f8d9e433f7a803d7150933895699f311d661c33b9729?__gda__=exp=1582004915~hmac=d776a1989eed6ad2732ec0839d2ef807438daf1d77951699ab6047beee82
######################################################################## 100.0%
==> Pouring pcre2-10.34.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/pcre2/10.34: 230 files, 5.9MB
==> Installing git
==> Downloading https://homebrew.bintray.com/bottles/git-2.25.0_1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/9a/9ab68a09b14009dd3c28fcd3e6ef98213f675887615cab5c67d62a026ccc019b?__gda__=exp=1582004918~hmac=99893c2225634c880e365f24ae3b81d89604a5cfda9e9f89e30d5aee33a5
######################################################################## 100.0%
==> Pouring git-2.25.0_1.catalina.bottle.tar.gz
==> Caveats
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
  /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git
==> Summary
🍺  /usr/local/Cellar/git/2.25.0_1: 1,443 files, 43.9MB
==> Caveats
==> gettext
gettext is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD gettext library & some software gets confused if both are in the library path.

If you need to have gettext first in your PATH run:
  echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.bash_profile

For compilers to find gettext you may need to set:
  export LDFLAGS="-L/usr/local/opt/gettext/lib"
  export CPPFLAGS="-I/usr/local/opt/gettext/include"

==> git
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
  /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git

これでインストールが完了されたはずです。

インストールされたgitの確認

Homebrewのgitのインストール確認

先程のbrew listコマンドで確認してみましょう。

$ brew list
gettext	git	pcre2  nodebrew

無事にgitが表示されました。

gitのバージョン確認

gitがインストールできたので、gitのバージョンを確認してみます。

$ git --version
git version 2.21.1 (Apple Git-122.3)
オーカミ

あれ?バージョンが変わってない??

gitのバージョンが正しく更新されている場合はこれでHomebrewからgitをインストールする一通りの手順は完了です。

もしversionが更新されていない場合は以下を確認してみてください。

git versionが更新されない場合

gitのバージョンが更新されてない場合は以下を試してみてください。

・ターミナルの再起動
・bash_profileにpathを追記

ターミナルの再起動

ターミナルの再起動をしてみて、再度git –versionを確認してみてください。

きっとちゃんとバージョンの更新がされるはずです。

bash_profileに追加

ターミナルの再起動でも更新されない場合は.bash_profileにexport PATH=/usr/local/bin:$PATHを追加してみてください。

$ vi ~/.bash_profile

開かれたbash_profileに[export PATH=/usr/local/bin:$PATH]を追記します。

export PATH=/usr/bin:$PATH

これで再度ターミナルを再起動してgit –versionを確認してみてください。

ちなみに/usr/bin/gitに古いgitが、/usr/local/bin/gitに新しいgitのパスとなるようで、PATHの優先順位が/usr/binのほうが先の場合に、古いGitを参照してしまうことがあるようです。

これで正しいgit のバージョンが表示されたんじゃ無いでしょうか?

$ git --version
git version 2.25.0

MacにHomebrewからgitをインストールまとめ

以上でmacにHomebrewからgitをインストールする方法になります。

基本的にはbrewでgitをインストールしただけでbrewのgitを参照するようになりますが、もしgitの参照が反映されない場合は、ターミナルや端末の再起動、.bash_profile等で参照先の変更を行ってみてください。

まとめ
  • Macにgitをインストールするのはbrew install git
  • gitのバージョンが更新されない時はターミナル再起動
  • それでもダメなら参照先を変更

おすすめ本

Homebrewについても書いてありますが、フロントエンジニアに必要なTipsが一通り網羅されて書いてあるのでオススメです。

よかったらシェアしてね!

コメント

コメントする

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

目次
閉じる