1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ディレクトリ単位でGitユーザーを自動で切り替える方法

1
Last updated at Posted at 2026-01-28

ディレクトリ単位でGitユーザーを自動で切り替える方法

複数の組織あるいは組織と個人でGitアカウントを複数使い分けるというシーンが多く有ると思うが、プロジェクトやディレクトリを横断するたびにGitユーザーを切り替えたり、sshやgitコマンドを打つ際にどのsshKeyを使うのかを指定するのが面倒なので作業ディレクトリでそれらを自動的に切り替えられるようにしようというのが今回の目標です。

前提

先ずはリモートアカウントと紐づけるためのssh keyを用意します。
ここは本題とずれるので完了した前提で進めます。
詳しいやり方を知りたい方は以下を参考にしてみてください。

GitHub アカウントへの新しい SSH キーの追加

ここでディレクトリ構造を確認します。
以下はサンプルですがこれをベースに進めていきます。

./user
├── .ssh
│   ├── key_1
│   ├── key_1.pub
│   ├── key_2
│   ├── key_2.pub
│   ├── key_3
│   ├── key_3.pub
│   └── config
├── Org-1
│   ├── Project-1
│   └── Project-2
└── Org-2
│   ├── Project-1
│   └── Project-2
└── Personal
    ├── Project-1
    └── Project-2

ssh config の設定

リモートアカウントと対応するsshkey を基材してHostにどのkeyがどの組織あるいはアカウントと紐付いているか判断できるようなラベリングをします。

Host 名は「github + 組織名」のように、用途が分かる名前を付けています。

.ssh/config
Host github.org1
  HostName github.com
  User git
  IdentityFile ~/.ssh/key_1
  IdentitiesOnly yes
  AddKeysToAgent yes

Host github.org2
  HostName github.com
  User git
  IdentityFile ~/.ssh/key_2
  IdentitiesOnly yes
  AddKeysToAgent yes

Host github.per
  HostName github.com
  User git
  IdentityFile ~/.ssh/key_3
  IdentitiesOnly yes
  AddKeysToAgent yes


.gitconfig の追加

ユーザーディレクトリ(~/)直下に.gitconfigを作成(すでにある場合は加筆・変更)します。

.gitconfig
[includeIf "gitdir:~/Org-1/"]
  path = ~/.gitconfig-org-1

[includeIf "gitdir:~/Org-2/"]
  path = ~/.gitconfig-org-2

[includeIf "gitdir:~/Personal/"]
  path = ~/.gitconfig-personal

gitdir: は「そのディレクトリ配下にある Git リポジトリでのみ、この設定を有効にする」という意味です。

ユーザー毎の.gitconfigの作成

先程.gitconfigで指定したユーザー別の.gitconfigを作成して行きます。
ユーザーディレクトリ(~/)直下に以下のファイル(今回は3つ)作成します。

.gitconfig-org-1
[user]
  name = my name 1
  email = your-mail@sample.email

[core]
  sshCommand = ssh -F ~/.ssh/config -o IdentitiesOnly=yes
.gitconfig-org-2
[user]
  name = my name 2
  email = your-mail@sample.email

[core]
  sshCommand = ssh -F ~/.ssh/config -o IdentitiesOnly=yes
.gitconfig-personal
[user]
  name = me
  email = your-mail@sample.email

[core]
  sshCommand = ssh -F ~/.ssh/config -o IdentitiesOnly=yes

remote URL の設定

各プロジェクト直下に移動してremoteコマンドを実行します。

通常、git@github.com:org1/RepositoryPath.git
であるところの@github.com@github.org1 に変更しています(ssh config の Host 名)

git remote set-url origin git@github.org1:org1/RepositoryPath.git

結果

これで設定は完了です。
以降、Org-1, Org-2, Personalディレクトリに移動すると自動的にgitユーザーが設定したものに切り替わります。

トラブルシューティング

既存リポジトリで user が切り替わらない場合

※ 既にリポジトリ内に user.name / user.email が設定されている場合、
そちらが優先されます。その場合は以下で削除してください。

git config --unset user.name
git config --unset user.email

また、includeIf の gitdir に ~ が効かない場合は/home/username/Org-1/ のように絶対パスを指定してください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?