LoginSignup
1
0

More than 3 years have passed since last update.

複数のgitアカウントの切り替え

Posted at

一つのPCで業務用、副業用、個人用など使い分けが発生するとき用のメモ。

globalの確認

メインで使うアカウントはgitの初期設定時にglobalで設定されているはず

$ cat ~/.gitconfig

[user]
    name = hoge
    email = hoge@example.com

$ git config --global user.name
hoge
$ git config --global user.email
hoge@example.com

globalを修正したいとき

$ git config --global user.name "huga"
$ git config --global user.email "huga@example.com"
$ cat ~/.gitconfig

[user]
    name = huga
    email = huga@example.com

特定のリポジトリでアカウント切り替えが必要なとき

$ git config --local user.name
hoge
$ git config --local user.email
hoge@example.com
$ git config --local user.name "huga"
$ git config --local user.email "huga@example.com"
$ git config --local user.name
huga
$ git config --local user.email
huga@example.com
$ cat ./.git/config

[user]
    name = huga
    email = huga@example.com

pushする前にgit logでどのアカウントでcommitされているか確認できる

$ git log
Author: huga <huga@example.com>

リポジトリにアクセスする際の手順

実際にリポジトリにアクセスする際にはホストの向き先の設定をしないと以下のようなエラーが発生した

ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

configに以下のように追記

~/.ssh/config
Host github.com-hoge
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_hoge_rsa

Host github.com-huga
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_huga_rsa

gitリポジトリのoriginのURLを確認する
デフォルトではgit@github.com:[Account name]/[Repository name].gitの形式になっている

$ git remote -v
origin  git@github.com:huga/example-app.git (fetch)
origin  git@github.com:huga/example-app.git (push)

gitリポジトリの向き先URLをconfigに追記したホスト名に変更する

$ git remote set-url origin git@github.com-huga:huga/example-app.git
1
0
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
1
0