LoginSignup
73
81

More than 1 year has passed since last update.

複数 git アカウントを1台のPCで使い分ける方法

Last updated at Posted at 2022-05-07

はじめに

複数 git アカウントを1つの PC で使い分けたいとき(「社用と私用両方から github 使いたい」等)、
社用と私用アカウントを使い分けたいとき(「社用アカウントで github, 私用アカウントで gitlab」等)、
ありますよね。手順まとめます!

重視した点
めちゃくちゃ細かく書きました。
自分が PC 新調して再設定する度に細かい所忘れ去っており思い出すのに苦労しているので(おっさんおつ)、
忘れ去っててもサクサク設定出来るように。。

やりたい構成

最初にイメージしやすく構成を書きます。
適当に、アカウント2つ(社用、私用)、それぞれに git ホスティングサービス2つ利用、の状況を考えます。
図にするとこんな感じ。
「アカウント1つ目で gitbucket と github、アカウント2つ目で github と gitlab」
としていますが、これはあくまで例で、実際やる場合の組合せは任意です。

$ cd ~/git
$ tree -a
.
├── .gitconfig
├── business
│   ├── .gitconfig
│   ├── gitbucket
│   │   └── repoA
│   └── github
│       └── repoB
└── personal
    ├── .gitconfig
    ├── github
    │   └── repoC
    └── gitlab
        └── repoD

メールアドレスとアカウント名は適当に、
1つめ(社用): business@example.com , account_business
2つめ(私用): personal@example.com , account_personal
にしましょうか。

手順

0. 準備

git v2.13.0 未満の人は v2.13.0 までバージョンを上げましょう。
本件は includeIf を使うのですが v2.13.0 以降で使用可能のためです。

1. SSH キーファイル生成

SSH キーファイル生成

$ cd ~/.ssh
$ ssh-keygen -t ed25519 -C "business@example.com" -f id_ed25519_github_business
$ ssh-keygen -t ed25519 -C "personal@example.com" -f id_ed25519_github_personal
$ ls -1
id_ed25519_github_business
id_ed25519_github_business.pub
id_ed25519_github_personal
id_ed25519_github_personal.pub

サービス側に公開鍵登録

github ならここから。
https://github.com/settings/keys

2. ディレクトリとファイル作成

$ cd ~/git
$ mkdir -p business/{gitbucket,github}
$ mkdir -p personal/{github,gitlab}
$ touch .gitconfig ./business/.gitconfig ./personal/.gitconfig
$ tree -a
.
|-- .gitconfig
|-- business
|   |-- .gitconfig
|   |-- gitbucket
|   `-- github
`-- personal
    |-- .gitconfig
    |-- github
    `-- gitlab

6 directories, 3 files

3. .gitconfig に設定追加

以下のように追記します。

$ cd ~/git
$ cat .gitconfig
[includeIf "gitdir:~/git/business"]
        path = ~/git/business/.gitconfig
[includeIf "gitdir:~/git/personal"]
        path = ~/git/personal/.gitconfig

$ cat business/.gitconfig
[user]
        name = account_business
        email = business@example.com

$ cat personal/.gitconfig
[user]
        name = account_personal
        email = personal@example.com

4. ~/.ssh/config 設定

以下のように設定します。

以下では business githubpersonal github のみ書き、
business gitbucketpersonal gitlab は略しちゃいますが、同じように設定します。

$ cd ~/.ssh
$ cat config
Host business.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_ed25519_github_business
  User git

Host personal.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_ed25519_github_personal
  User git

5. 個別リポジトリで git clone

社用アカウント

$ cd ~/git/business/github
$ git clone git@business.github.com:account_business/repoB.git
// 意味合い的には以下
//git clone git@github.com:account_business/repoB.git

私用アカウント

$ cd ~/git/personal
$ git clone git@personal.github.com:account_personal/repoC.git
// 意味合い的には以下
//git clone git@github.com:account_personal/repoC.git

git config でアカウント確認

「社用 PC にて、基本社用アカウント、部分的に私用アカウントで git clone したい」
場合の例は以下です。

// 基本は社用アカウントに設定
$ git config --global user.name "account_business"
$ git config --global user.email "business@example.com"

// 私用アカウント使いたい場所では私用アカウントに設定
$ cd ~/git/personal/github/repoC
$ git config --local user.name "account_personal"
$ git config --local user.email "personal@example.com"

これで無事、当初やりたかった
「基本的には社用アカウントが使われ、私用アカウント用の personal ディレクトリ以下のみで私用アカウントが使われる」
状態になります。めでたし!

// どのディレクトリでも global 設定は社用アカウント
$ git config --global --list
user.name=account_business
user.email=business@example.com

// global より local 設定が優先なので personal ディレクトリ以下は私用アカウントが有効
$ cd personal/github/repoC
$ git config --local --list
user.name=account_personal
user.email=personal@example.com
$ git config --global --list
user.name=account_business
user.email=business@example.com

注意点

github の利用規約上、「無料アカウントは1個まで」の記載があるので、
無料アカウントの利用は正しく計画的に行きましょう!
「社用アカウントは有料、私用アカウントは無料」等はセーフですね。

利用規約 英語

One person or legal entity may maintain no more than one free Account (if you choose to control a machine account as well, that's fine, but it can only be used for running a machine).

利用規約 日本語

1 人の個人または 1 つの法人が複数の無料「アカウント」を保持することはできません (コンピュータアカウントも制御することを選択した場合、それは問題ありませんが、それはコンピュータの実行にのみ使用できます)。

参考

73
81
3

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
73
81