コンテンツにスキップ

Git & GitHub セットアップ

chroはバージョン管理にGitを、コラボレーションにGitHubを使用します。このガイドでは、両プラットフォームでのインストールとSSH鍵のセットアップについて説明します。

まず、Gitがすでにインストールされているか確認します:

Terminal window
git --version

Gitがインストールされていれば、バージョン番号が表示されます。そうでない場合は、以下のインストール手順に従ってください。


方法1: Xcode Command Line Tools(推奨)

Section titled “方法1: Xcode Command Line Tools(推奨)”

macOSでGitを入手する最も簡単な方法:

Terminal window
xcode-select --install

ダイアログが表示されます。「インストール」をクリックして完了を待ちます。

Homebrewがインストールされている場合:

Terminal window
brew install git
Terminal window
git --version # git version 2.x.x と表示されるはず

Windowsパッケージマネージャーを使用:

Terminal window
winget install Git.Git

注意: インストール後、ターミナルを再起動してください。

方法2: Git for Windows インストーラー

Section titled “方法2: Git for Windows インストーラー”
  1. git-scm.comからダウンロード
  2. インストーラーを実行
  3. デフォルト設定を使用(初心者には推奨)
  4. 「Git from the command line and also from 3rd-party software」が選択されていることを確認

新しいターミナル/PowerShellウィンドウを開きます:

Terminal window
git --version

ID(コミットに必要)を設定します:

Terminal window
git config --global user.name "あなたの名前"
git config --global user.email "[email protected]"

デフォルトブランチ名をmainに設定:

Terminal window
git config --global init.defaultBranch main

設定を確認:

Terminal window
git config --list

GitHubアカウントのセットアップ

Section titled “GitHubアカウントのセットアップ”

GitHubアカウントをお持ちでない場合:

  1. github.comにアクセス
  2. 「Sign up」をクリック
  3. 登録プロセスに従う
  4. メールアドレスを確認

SSH鍵は、GitHubとのセキュアでパスワード不要の認証を提供します。

macOS / Linux / Windows(Git BashまたはWSL):

Terminal window
ssh-keygen -t ed25519 -C "[email protected]"

プロンプトが表示されたら:

  • Enterキーを押してデフォルトのファイル場所を受け入れる
  • パスフレーズを入力(推奨)またはEnterキーでパスフレーズなし

Windows(PowerShell):

Terminal window
ssh-keygen -t ed25519 -C "[email protected]"

macOS:

Terminal window
eval "$(ssh-agent -s)"

~/.ssh/configに追加(存在しない場合は作成):

Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519

鍵を追加:

Terminal window
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Windows(PowerShell - 管理者として実行):

Terminal window
# ssh-agentサービスを開始
Get-Service -Name ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
# 鍵を追加
ssh-add $env:USERPROFILE\.ssh\id_ed25519

Linux / WSL:

Terminal window
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

1. 公開鍵をコピー:

macOS:

Terminal window
pbcopy < ~/.ssh/id_ed25519.pub

Windows(PowerShell):

Terminal window
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard

Linux:

Terminal window
cat ~/.ssh/id_ed25519.pub
# 出力を手動でコピー

2. GitHubに追加:

  1. GitHub設定 > SSH Keysにアクセス
  2. 「New SSH key」をクリック
  3. わかりやすいタイトルを入力(例:「MacBook Pro 2024」)
  4. 「Key」フィールドに鍵を貼り付け
  5. 「Add SSH key」をクリック
Terminal window

以下のように表示されるはずです:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

代替手段: 個人アクセストークンを使用したHTTPS

Section titled “代替手段: 個人アクセストークンを使用したHTTPS”

SSHよりHTTPSを好む場合:

  1. GitHub設定 > Developer Settings > Personal Access Tokensにアクセス
  2. 「Generate new token (classic)」をクリック
  3. わかりやすい名前を入力
  4. スコープを選択:repo(プライベートリポジトリの完全な制御)
  5. 「Generate token」をクリック
  6. トークンをすぐにコピー(再度表示されません)

macOS:

Terminal window
git config --global credential.helper osxkeychain

Windows:

Terminal window
git config --global credential.helper wincred

初めてpush/pullするとき、GitHubユーザー名を入力し、パスワードとしてトークンを使用します。


リポジトリをクローンしてテスト:

Terminal window
# SSH(推奨)
git clone [email protected]:octocat/Hello-World.git
# またはHTTPS
git clone https://github.com/octocat/Hello-World.git

成功すれば、GitでGitHubを使用する準備ができています!


  • SSH鍵がエージェントに追加されていることを確認:ssh-add -l
  • 鍵がGitHubに追加されていることを確認:github.com/settings/keys
  • 正しい鍵を使用していることを確認:ssh -vT [email protected]

”Could not open a connection to your authentication agent”

Section titled “”Could not open a connection to your authentication agent””

SSHエージェントを起動:

Terminal window
eval "$(ssh-agent -s)"

Gitがpushのたびにパスワードを要求(HTTPS)

Section titled “Gitがpushのたびにパスワードを要求(HTTPS)”

認証情報ストレージを設定:

Terminal window
# macOS
git config --global credential.helper osxkeychain
# Windows
git config --global credential.helper wincred
# Linux
git config --global credential.helper store

改行を処理するようにGitを設定:

Terminal window
git config --global core.autocrlf true

GitとGitHubの設定が完了したら、次に進んでください: