Hey, awesome developer! Do you have multiple GitHub accounts and find it a headache to manage them on the same machine? Don't worry! Here's a step-by-step guide to set up your SSH keys and use different profiles for each project. Let's get started!
Step 1: Create an SSH Key for Each Account
First, make sure you're in your .ssh directory. If you don't know where it is, don't worry, Git does.
$ cd ~/.ssh
The magic command to generate a unique SSH key for each account is this:
ssh-keygen -t rsa -C "your email" -f "your key name"
- -C: A comment to identify your SSH key.
- -f: The file name where your SSH key will be saved.
Example:
ssh-keygen -t rsa -C "work@example.com" -f "id_work"
ssh-keygen -t rsa -C "personal@example.com" -f "id_personal"
Now you have two SSH keys, one for your work account and one for your personal account.
Step 2: Add the SSH Keys to the SSH Agent
Generating keys is great, but we need to tell the SSH agent to use them.
ssh-add -K ~/.ssh/id_work
ssh-add -K ~/.ssh/id_personal
Boom! Now your keys are ready to work.
Step 3: Add the SSH Public Key to GitHub
Time to tell GitHub who you are. Copy the contents of your public keys.
cat ~/.ssh/id_work.pub
cat ~/.ssh/id_personal.pub
Next steps:
- Log in to your GitHub account.
- Go to Settings > SSH and GPG keys > New SSH Key.
- Paste your public key and give it a title you like.
Step 4: Create a Config File and Make Host Entries
The ~/.ssh/config
file lets us specify many configuration options for SSH.
If you don't have a config file, create one.
nano config
Add these lines, each block for each account:
#Work account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_work
IdentitiesOnly yes
#Personal account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
IdentitiesOnly yes
- Git Configuration
First, let's change the default Git editor to VSCode. It's optional, but who doesn't love a good GUI editor?
git config --global core.editor 'code --wait'
Let's also create two aliases for easily accessing and editing Git configurations.
alias gcg="git config --edit --global"
alias gcl="git config --edit --local"
- Organize Your Projects
Let's organize our projects into different folders so each uses the appropriate configuration.
~
├── .gitconfig <-- global
└── main-folder/
├── personal/
│ ├── repo_1/
│ ├── repo_2/
│ └── .gitconfig <-- personal
└── work/
├── repo_1/
├── repo_2/
└── .gitconfig <-- work
- Personal Configuration
# ~/main-folder/personal/.gitconfig
[user]
name = Your Personal Name
email = personal@example.com
[core]
sshCommand = "ssh -i ~/.ssh/id_personal -F /dev/null"
- Work Configuration
# ~/main-folder/work/.gitconfig
[user]
name = Your Work Name
email = work@example.com
[core]
sshCommand = "ssh -i ~/.ssh/id_work -F /dev/null"
- Global Configuration
Let's open the global configuration and add the specific paths for our projects, by typing:
git config --edit --global
#~/.gitconfig
[core]
editor = code --wait
[includeIf "gitdir:~/main-folder/personal/"]
path = ~/main-folder/personal/.gitconfig
[includeIf "gitdir:~/main-folder/work/"]
path = ~/main-folder/work/.gitconfig
And that's it! Now Git will use the correct user configuration based on the project you're working on. Clone your repositories into the appropriate folders and make sure to use the right commands to verify the configuration.
git config user.name
Congratulations! Now you're ready to manage multiple GitHub accounts on the same machine like a pro. If you have any problems, just make sure the paths are correct, Happy coding! 🚀
Top comments (0)