MacでGitを使えるようにセットアップする
はじめに
こんにちは。
コードを本格的に勉強しよう!と思いまして、まずは形からということで普段使用しているPC(mac)にGitを導入し、SSHできるように設定してみようと思います。
この記事でやらないこと
以下の項目は実施済みの前提とします。
- Homebrewのインストール
- Githubアカウントの取得
この記事のゴール
手持ちのMacからGitthubにSSHアクセスができること
Macのターミナル上からGithubにSSHアクセスの確認が成功するところまでを実施します。
gitコマンドのインストール
1.Git公式サイトへアクセスし、「Download for macOS」を押下します。
2.brewコマンドをコピーし、Macのターミナル上で実行します。
(つまり手順1は飛ばしてもよかったということです。すみません)
コマンド
brew install git
実行結果例
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:d9faa506c014dedc0b034a68103ba75c9a58242f4d6c67b6ca0f649c39602bcf
######################################################################### 100.0%
==> Pouring portable-ruby-3.3.7.arm64_big_sur.bottle.tar.gz
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
中略
You have 7 outdated formulae installed.
==> Downloading https://ghcr.io/v2/homebrew/core/git/manifests/2.48.1
######################################################################### 100.0%
==> Fetching dependencies for git: libunistring, gettext and pcre2
==> Downloading https://ghcr.io/v2/homebrew/core/libunistring/manifests/1.3
######################################################################### 100.0%
==> Fetching libunistring
==> Downloading https://ghcr.io/v2/homebrew/core/libunistring/blobs/sha256:38e44
######################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/manifests/0.23.1
######################################################################### 100.0%
==> Fetching gettext
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/blobs/sha256:f3b350830a
######################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/manifests/10.44
######################################################################### 100.0%
==> Fetching pcre2
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/blobs/sha256:78fe8885f70c
######################################################################### 100.0%
==> Fetching git
==> Downloading https://ghcr.io/v2/homebrew/core/git/blobs/sha256:eddc3aeee81a51
######################################################################### 100.0%
==> Installing dependencies for git: libunistring, gettext and pcre2
==> Installing git dependency: libunistring
==> Downloading https://ghcr.io/v2/homebrew/core/libunistring/manifests/1.3
Already downloaded: /Users/shimizuyuuki/Library/Caches/Homebrew/downloads/a570da63bc1839c7e217f203abd54d4d873ebd6b99f6e88994d0e79e2ebe987c--libunistring-1.3.bottle_manifest.json
==> Pouring libunistring--1.3.arm64_sonoma.bottle.tar.gz
🍺 /opt/homebrew/Cellar/libunistring/1.3: 59 files, 5.4MB
==> Installing git dependency: gettext
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/manifests/0.23.1
Already downloaded: /Users/shimizuyuuki/Library/Caches/Homebrew/downloads/a5b6ba6453cc31731ac67fb3d0075d98ab572a913f3e740b54a86d79906f360e--gettext-0.23.1.bottle_manifest.json
==> Pouring gettext--0.23.1.arm64_sonoma.bottle.tar.gz
🍺 /opt/homebrew/Cellar/gettext/0.23.1: 2,052 files, 22.2MB
==> Installing git dependency: pcre2
==> Downloading https://ghcr.io/v2/homebrew/core/pcre2/manifests/10.44
Already downloaded: /Users/shimizuyuuki/Library/Caches/Homebrew/downloads/22ed791461c5bf400adde8c3b432c1230866aa1db3c5cb81e06a6ff21cac96ee--pcre2-10.44.bottle_manifest.json
==> Pouring pcre2--10.44.arm64_sonoma.bottle.tar.gz
🍺 /opt/homebrew/Cellar/pcre2/10.44: 237 files, 6.3MB
==> Installing git
==> Pouring git--2.48.1.arm64_sonoma.bottle.tar.gz
==> Caveats
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.
Subversion interoperability (git-svn) is now in the `git-svn` formula.
zsh completions and functions have been installed to:
/opt/homebrew/share/zsh/site-functions
==> Summary
🍺 /opt/homebrew/Cellar/git/2.48.1: 1,699 files, 54.8MB
==> Running `brew cleanup git`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> git
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.
Subversion interoperability (git-svn) is now in the `git-svn` formula.
zsh completions and functions have been installed to:
/opt/homebrew/share/zsh/site-functions
3.gitのバージョンを確認します。
コマンド
git -v
実行結果例
git version 2.39.5 (Apple Git-154)
初期設定
1.Gitにユーザー情報を登録します。
git config --global user.name "ユーザー名"
git config --global user.email "メールアドレス"
2.Gitに登録するためのSSH鍵を生成します。
コマンド
ssh-keygen -t rsa -b 4096 -C "yuu6446@gmail.com"
実行結果例
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxxx/.ssh/id_rsa
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:vo/5lifpCxKX5QwaKaV74IywgSjAuCTrF8pSiyWq8ps yuu6446@gmail.com
The key's randomart image is:
+---[RSA 4096]----+
|+ . |
|*o o . |
|Oo + o . . |
|=+o= + o * |
|==o.= + S o |
|++.. . + |
|o . . o o |
|o . . == . |
|..E. +==+ |
+----[SHA256]-----+
確認コマンド(一部抜粋)
ls -l /Users/xxxx/.ssh/
total 48
-rw------- 1 xxxx staff 3381 2 24 08:19 id_rsa
-rw-r--r-- 1 xxxx staff 743 2 24 08:19 id_rsa.pub
3.公開鍵の中身を確認し、控えておきます。(この後の手順で使用します)
cat /Users/xxxx/.ssh/id_rsa.pub
ssh-rsa 略
以下、Githubの画面上で操作を行います。
1.Githubにログインしたら画面右上のアイコンを押下し、「Settings」を開きます。
2.画面左の一覧から「SSH and GPG keys」を選択します。
3.各項目を入力し、「Add SSH key」を押下します。
title 任意のタイトル
Key type Authentication Key
Key 「初期設定」の手順3で控えておいた公開鍵の内容をペーストします。
4.SSH接続確認
コマンド
ssh -T git@github.com
実行結果例(質問にはyesと入力してEnter)
The authenticity of host 'github.com (64:ff9b::141b:b171)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi yu-shi-64! You've successfully authenticated, but GitHub does not provide shell access.
最後に
今回はGithubにSSH接続できるようになるまでの手順を掲載しました。
Gitは以前少しだけ使っていたのですが、手順としてまとめていなかったのと鍵についても理解できていなかったので今回改めて理解しながら手順を実施できてよかったです。
Discussion