This repository provides a step-by-step guide on how to set up and manage multiple GitHub accounts on a single laptop using SSH authentication. It is designed to help developers, students, and professionals who need to work with multiple GitHub accounts seamlessly.
ssh-keygen -t rsa -b 4096 -C "[email protected]"
- Save it as
~/.ssh/id_rsa_second
- When prompted, enter a passphrase (or leave empty for no passphrase).
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_second
- Copy the public key:
cat ~/.ssh/id_rsa_second.pub
- Go to GitHub → Settings → SSH and GPG keys → New SSH Key
- Paste the copied key and save.
Edit (or create) the SSH config file:
nano ~/.ssh/config
Add the following lines:
# First GitHub Account
Host github.com-first
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_first
# Second GitHub Account
Host github.com-second
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_second
Save and exit (Ctrl + X
, then Y
, then Enter
).
ssh -T [email protected]
✅ Expected output:
Hi YourUsername! You've successfully authenticated, but GitHub does not provide shell access.
git clone [email protected]:YourSecondUsername/YourRepo.git
- Replace
YourSecondUsername
andYourRepo
with your actual GitHub username and repo name.
cd YourRepo
git remote -v
✅ Expected output:
origin [email protected]:YourSecondUsername/YourRepo.git (fetch)
origin [email protected]:YourSecondUsername/YourRepo.git (push)
Since the global config still points to the first account, we need to configure Git locally for this specific repo.
git config user.name "YourSecondUsername"
git config user.email "[email protected]"
git config --list --local
✅ Expected output:
user.name=YourSecondUsername
[email protected]
echo "Testing new setup for second GitHub account" >> test-second.txt
git add test-second.txt
git commit -m "Test commit for second GitHub account"
git push origin main
✅ If successful, you should see the commit appear on GitHub!
- Now, you can seamlessly switch between accounts by navigating to the appropriate repo.
- All future commits in the second account's repo will use the second GitHub account.
- To switch accounts, just navigate between different repositories configured for each account.
If you cloned a repository from your second GitHub account but want to push it to your first GitHub account, you need to change the remote URL from the second account to the first account. Here’s how you do it:
Run this in the cloned repo:
git remote -v
✅ Output should look like:
origin [email protected]:SecondAccountUserName/git-remote.git (fetch)
origin [email protected]:SecondAccountUserName/git-remote.git (push)
This confirms that the remote is linked to your second GitHub account.
Since you want to push this repo to your first account, you need to remove the existing remote:
git remote remove origin
- Go to GitHub → Your First Account.
- Click New Repository.
- Give it a name (e.g.,
git-remote-first
). - Do NOT initialize with README, .gitignore, or a license (since you already have a repo locally).
- Copy the SSH URL (it should look like this):
[email protected]:FirstAccountUsername/git-remote-first.git
Now, link your local repo to your first GitHub account:
git remote add origin [email protected]:FirstAccountUsername/git-remote-first.git
Verify it:
git remote -v
✅ Expected output:
origin [email protected]:FirstAccountUsername/git-remote-first.git (fetch)
origin [email protected]:FirstAccountUsername/git-remote-first.git (push)
git push -u origin main
✅ If successful, your repository is now in your first GitHub account!
✔️ Clone the repo from the second account
✔️ Remove the existing remote (git remote remove origin
)
✔️ Create a new repo on the first account
✔️ Add the new remote from the first account
✔️ Push your local repo to the first account