If you have multiple GitHub accounts and want to manage them from a single MacOS machine, follow these steps:
- MacOS/Linux has an SSH configuration file located in the
.ssh
directory. - Navigate to it by running
cd ~/.ssh
in your terminal.
- If you have, for instance, 2 GitHub accounts (one for work and one personal), you need to create SSH key pairs for each.
- Run:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- You'll be prompted to enter a filename to save the key. Ensure it's unique, e.g.,
user-1
,user-2
.
- Register the generated SSH keys on the respective GitHub accounts.
- Follow these steps to do so.
- Go back to the SSH config file in
~/.ssh
and modify it as:# user1 account Host github.com-user1 HostName github.com User git IdentityFile ~/.ssh/github-user1 IdentitiesOnly yes # user2 account Host github.com-user2 HostName github.com User git IdentityFile ~/.ssh/github-user2 IdentitiesOnly yes
- Run:
git clone git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
- Open local git config using
git config --local -e
and add:[user] name = user1 email = user1@gmail.com
- Example:
git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
- Set the remote URL using:
git remote set-url origin git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
Your local repository's .git/config
file would look like:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@github.com-user1:user1/your-repo-name.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[user]
name = user1
email = user1@gmail.com