Featured image of post 複数githubアカウントの管理

複数githubアカウントの管理

目次

背景

  • 毎回迷うのでメモをのこす
  • この3段階が一番キレイ

~/.gitconfig

これはdotfilesとかで管理されるglobalなgitconfig。

1
2
[include]
  path = ~/.gitconfig.local

~/.gitconfig.local

これはdotfilesでは管理しないローカルなgitconfig。

1
2
[includeIf "gitdir:/media/mike/xxx/"]
  path = ~/.gitconfig.xxx

この例では、あるmediaのxxx以下のgitのフォルダは、gitconfig.xxxを使用するという意味。

~/.gitconfig.xxx

そのある専用のフォルダで使われるgitconfig。

1
2
3
4
5
6
7
[user]
  name = xxx_hoge
  email = xxx@hoge
[commit]
  gpgsign = false
[core]
  sshCommand = ssh -i ~/.ssh/id_xxx_rsa -o IdentitiesOnly=yes

gpgはoffにしたり、usernameを変えたり、sshの設定を入れ替えたりしている。

テスト

その特定のフォルダの中でちゃんとworkしているかのテストは以下になる。

1
2
3
4
5
6
7
cd /medi/mike/xxx 

git config --get user.name
xxx_hoge

git config --get user.email
xxx@hoge

sshの設定

それ専用に用意する。

1
2
3
4
5
Host github-hoge
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_xxx
    IdentitiesOnly yes

Clone:

1
git clone git@github-hoge:hoge-org/hoge.git

NOTE: もしくは、git@github.com:hoge-org/hoge.gitでもいけるかもしれない。

gh cli

まずはghのログイン。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
gh auth login
? Where do you use GitHub? GitHub.com
? What is your preferred protocol for Git operations on this host? SSH
? Upload your SSH public key to your GitHub account? /home/mike/.ssh/id_xxx_rsa.pub
? Title for your SSH key: hogehoge 
? How would you like to authenticate GitHub CLI? Login with a web browser
                                                
gh auth status
github.com
  ✓ Logged in to github.com account xxx
  - Active account: true
  - Git operations protocol: ssh

ghコマンドに関しては、ログイン後にGH Oauthトークンを発行する。

1
2
gh auth token --user hoge --hostname github.com
gho_xxxxx

これを.envrcに保存して特定のフォルダの中ではそのOAUTHを使うようにする。

1
2
3
4
# gh CLI: このディレクトリ配下だけ hogeアカウントを使う
# GH_TOKEN はグローバルの active account (gh auth switch) より優先されるので、
# ここを出れば自動的に (このファイルの外の) 既定アカウントに戻る。
export GH_TOKEN=gho_xxxxx

まとめ

毎回の儀式。

Built with Hugo
テーマ Stack は Jimmy によって設計されています。