0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

業務・プライベートでGitHubアカウントを使い分ける設定方法

Last updated at Posted at 2025-08-22

初めに

業務で複数案件を掛け持ちしたり、業務とプライベートでGitアカウントを使い分けたい場合がありますよね。
しかし毎回configファイルを編集するのは大変です。

そこで、私がUbuntu環境で実際に使っている効率的なアカウント切り替え方法を、備忘録も兼ねてまとめました。
後発の方の参考になれば幸いです。

手順

①メインアカウントとサブアカウントのSSH Keyを作成する

"git SSH"などで調べれば記事が出てきます。
各アカウントにSSH公開鍵の登録まで完了させてください。

②configファイルを編集

ファイル内容を以下のようにしてください。
コマンドを実行する際でHostの部分の名称が重要となるので注意してください。

Host main
    HostName github.com
    IdentityFile ~/.ssh/メインアカウントの秘密鍵のファイル名
    User git
    IdentitiesOnly yes

Host sub
    HostName github.com
    IdentityFile ~/.ssh/サブアカウントの秘密鍵のファイル名
    User git
    IdentitiesOnly yes

③.bashrc

.bashrcを編集

~配下にある、.bashrcに以下の内容を追記します。
ファイルが無かったら作成してください。

gitmain() {
  git config --global user.name "メインアカウント名"
  git config --global user.email "メインアカウントのメールアドレス"
}

gitsub() {
  git config --global user.name "サブアカウント名"
  git config --global user.email "サブアカウントメールアドレス"
}

check() {
  git config user.email
  git config user.name
}

.bashrcの変更を適用

source ~/.bashrc

アカウント切替え確認

# メインアカウント切替
gitmain
check
# →メインアカウントの情報が表示される
ssh -T git@main
# →Hi メインアカウント名! You've successfully authenticated, but GitHub does not provide shell access.

# サブアカウント切替
gitsub
check
# →サブアカウントの情報が表示される
ssh -T git@sub
# →Hi サブアカウント名! You've successfully authenticated, but GitHub does not provide shell access.

リポジトリのクローン

praivateリポジトリをクローンする際以下のようなコマンドにする必要があります。

## メインアカウント
git clone git@main:username/repository.git

## サブアカウント
git clone git@sub:username/repository.git

コミット・プッシュ時

メインアカウント

# 1. アカウント切り替え
gitmain

# 2. 設定確認
check

# 3. 通常通りの作業
git add .
git commit -m "コミットメッセージ"
git push origin main

サブアカウント

# 1. アカウント切り替え
gitsub

# 2. 設定確認
check

# 3. 通常通りの作業
git add .
git commit -m "コミットメッセージ"
git push origin main

おわりに

複数アカウントの切り替えが簡単になることで、開発効率が向上すると思います。
同じような課題でお困りの方の参考になれば幸いです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?