Using multiple git identities on MacOS

Update: This works too. https://stackoverflow.com/a/46393420/1198897

git config --local core.sshCommand "ssh -i /Users/xxxx/.ssh/id_rsa_another_identity"

Setup Identity

(1) Generate an additional key pair for your new identity.

cd ~/.ssh
ssh-keygen -t rsa -C "name@company.com" -f "id_rsa_company"

(2) Add the new key into SSH agent.

ssh-add -K ~/.ssh/id_rsa_company

(3) Copy the public key.

pbcopy < ~/.ssh/id_rsa_company.pub

(4) Sign-in to your new bitbucket account. Go to "Account" -> "Bitbucket Settings" -> "SSH Keys". Enter your public key here.


(5) Update the SSH Config file on your Mac.

pico ~/.ssh/config
#username1
Host bitbucket.username1
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/id_rsa
     IdentitiesOnly yes

#username2
Host bitbucket.username2
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/id_rsa_company
     IdentitiesOnly yes

Using the New Identity

(1) In the directory which you wish to use the new, set the git user name and email.

git config user.name "username2"
git config user.email "name@company.com"

(2) Check your config.

git config --list

(3) Test commit and check author detail.

git log

(4) Push to remote.



AI Summary
Chrome On-device AI 2024-10-04 23:05:18

Share Article