xChar

最近在工作的電腦上需要使用不同的Github account來存取不同的Repo,在踩了一些雷後終於完成設定,現在可以自由的切換account來存取不同的Repo了。

  1. 為各自的帳號建立ssh key
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github_a

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github_b
  1. 將公鑰(github_a.pub, github_b.pub)新增到各自的Github > settings > SSH and GPG keys
  2. Start the ssh-agent in the background.
$ eval "$(ssh-agent -s)"
  1. 將剛剛產生的key的私鑰加進去ssh中
ssh-add ~/.ssh/github_a
ssh-add ~/.ssh/github_b
  1. 編輯~/.ssh/config
vim ~/.ssh/config
Host gtihub_q
  HostName github.com
  AddKeysToAgent yes
  User git
  UseKeychain yes
  IdentityFile ~/.ssh/github_a

Host github_b
  HostName github.com
  AddKeysToAgent yes
  User git
  UseKeychain yes
  IdentityFile ~/.ssh/github_b
  1. 使用指令來登入並clone專案
ssh -T git@github_a
git clone git@github_a:XXXXX/XXXXX.git

這邊滿重要的是你在clone 時去修改repo給你的預設指令
git clone [email protected]:XXXXX/XXXXX.git
改成git clone git@github_a:XXXXX/XXXXX.git

  1. 如果是已經clone 的專案就需要修改remote的來源,將原本來源
origin  [email protected]:xxxxx/xxxxx.git (fetch)
origin  [email protected]:xxxxx/xxxxx.git (push)

改成

origin  git@github_a:xxxxx/xxxxx.git (fetch)
origin  git@github_a:xxxxx/xxxxx.git (push)

這樣就可以不用在一直切帳號來存取不同的repo了。

Loading comments...