1台のPCで複数のGitHubアカウントを使い分けることは可能か?
下記の手順で可能です
- Gitの設定
- GitHubアカウント作成
- あたらしく作成したGit用のSSH鍵を作成
Gitの設定
Gitの設定確認
Gitのグローバル設定で、現在のユーザー名、メールアドレスを表示して確認する
git config --global user.name
git config --global user.email
上記とは違う設定を使用する管理方法でします。
グローバルはプライベート用で、それとは別で仕事用を作成するイメージです
もう1つのGitHubアカウントを作る
共通ディレクトリで新しい別のGit設定
(新しいGitの設定を各リポジトリごとに設定する方法もありますが、今回は共通ディレクトリに設定します)
まずはグローバルの設定ファイルに下記の追記
~/.gitconfig
[user]
name = LAPTOP-2C4PL9J8\\jingt
email = 10121012tt@gmail.com
[difftool "sourcetree"]
cmd = "'' "
[mergetool "sourcetree"]
cmd = "'' "
trustExitCode = true
[core]
autoCRLF = false
# 仕事用リポジトリの設定を読み込む
[includeIf "gitdir:C:/work/"]
path = ~/.gitconfig_work
gitdir:C:/work/
というパスにある Gitリポジトリの中で作業しているときだけ~/.gitconfig_work
という 追加設定ファイルを読み込む
Windowsの場合パスは絶対で記述
記述方法 | 動く環境 | 解説 |
---|---|---|
gitdir:~/.../ | macOS / Linux ✅ | ~ をシェルが解釈するからOK |
gitdir:C:/Users/.../ | Windows ✅ | 絶対パスを書くのが確実! |
個人アカウント用のプロファイルを作成(~/.gitconfig_work)
# 仕事用GitHubアカウント設定
[user]
name = "xxxx-work"
email = "xxxx@gmail.com"
新しいアカウントで使いたいリポジトリのフォルダ群を含む共通ディレクトリへ移動
cd path/to/your/newgitfolder
下記で設定を確認すると
git config user.name
git config user.email
グローバルの設定と同じです、、あれ!となりますがgit initすることで
新しいく設定したgitの設定を確認できます。
PS C:\work\test> git config user.name
LAPTOP-xxxx\username
PS C:\work\test> git init
Initialized empty Git repository in C:/work/test/.git/
PS C:\work\test> git config user.name
xxxx-work
なぜ git init が必要なのか?
includeIf “gitdir:…” は 「Gitリポジトリ内に入ったときだけ」 有効になる仕組み
だから、.git ディレクトリがない場所では何も切り替わらない!
項目 | 例 | 説明 |
---|---|---|
GitHubユーザー名 | yamada-taro | GitHubにログインするためのID |
GitHubメール | taro.yamada@example.com | GitHubに登録してるメールアドレス |
Git user.name | 山田 太郎 (または yamada-taro ) | コミット作者として表示される名前 |
Git user.email | taro.yamada@example.com | コミット作者のメール(GitHubと一致推奨) |
SSH鍵の設定
- サブアカウント用のSSH鍵を生成
どこのディレクトリでもOK
PS C:\work> ssh-keygen -t ed25519 -C "your-email@gmail.com" -f ~/.ssh/id_ed25519_github_work
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
// 「パスフレーズ(鍵のロック)をつけますか?」
// 空のまま Enterで開発環境ではこれが多い(毎回入力なし)
Your identification has been saved in C:\Users\username\.ssh\id_ed25519_github_work
Your public key has been saved in C:\Users\username\.ssh\id_ed25519_github_work.pub
The key fingerprint is:
SHA256:xxxxxx your-email@gmail.com
The key's randomart image is:
+--[ED25519 256]--+
| |
| |
| |
| |
| |
| |
| |
+----[SHA256]-----+
下記で公開鍵を表示してGithubにコピペ
Get-Content C:\Users\username\.ssh\id_ed25519_github_work.pub
- .ssh/configファイルの設定
# GitHub ワークアカウント用 SSH 設定
Host github.com-ida-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_work
IdentitiesOnly yes
テスト
PS C:\work> ssh -T git@github.com-xxxx
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.
- リモートURLを確認
git remote -v
- 問題なければpush
git push -u origin main
SSH鍵を使ってリモートURLを指定する(403エラー対策)
複数アカウントを使い分けていて、
git push 時に「403」エラーや他アカウント扱いになる場合
プッシュ時の認証がおかしい場合があります
リモートURLの形式を確認する
git remote -v
↓こんな感じになっていたら、HTTPS形式なので修正が必要です
origin https://github.com/work/fastapi-apprunner.git (fetch)
origin https://github.com/work/fastapi-apprunner.git (push)
SSHに変更する(しかもアカウント切替対応のHost指定で)
git remote set-url origin git@github.com:<ユーザー名>/<リポジトリ名>.git
設定後の確認とpush
git remote -v # URLが変わったか確認
git push -u origin main