blog icon indicating copy to clipboard operation
blog copied to clipboard

Work With Two Git/GitHub Accounts On One Computer

Open qingquan-li opened this issue 2 years ago • 1 comments

1. Create SSH Key

Generate a unique SSH key for second GitHub account.

$ ssh-keygen -t ed25519 -C "[email protected]"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/username/.ssh/id_ed25519): id_ed25519_account2
...

2. Add the New SSH key to the Second GitHub Account

Copy the SSH public key (~/.ssh/id_ed25519_account2.pub) to the second GitHub account: https://github.com/settings/keys

3. Add the SSH Key to the Agent

$ ssh-add ~/.ssh/id_ed25519 # default SSH key
Identity added: /Users/username/.ssh/id_ed25519 ([email protected])
$ ssh-add ~/.ssh/id_ed25519_account2
Identity added: /Users/username/.ssh/id_ed25519_account2 ([email protected])

4. Configure the config File

$ vim ~/.ssh/config
#Default GitHub Account
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519

Host github.com-account2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_account2

5. Change Remote Repository URL

Use [email protected] instead of [email protected]

$ git remote set-url origin [email protected]:your-github-username/repository-name.git

6. Change (Local) Git Username and Email

If you have set the global config,

$ git config --global user.name "your-name-of-account1"
$ git config --global user.email [email protected]

then you can change the user.name and user.email locally for the current repository.

$ git config user.name "your-name-of-account2"
$ git config user.email "[email protected]"

qingquan-li avatar Oct 01 '22 04:10 qingquan-li