If you want to add a new GitHub origin with new SSH keys for just one project without affecting your existing projects, Here's how you can do it:
-
Generate a New SSH Key: If you haven't already, generate a new SSH key for the new GitHub account. You can do this with the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Replace
your_email@example.com
with your email address. When prompted, specify a file name for the new key (e.g.,id_rsa_newuser
). -
Add the New SSH Key to Your GitHub Account: Copy the public key (usually
id_rsa_newuser.pub
) and add it to your GitHub account under "Settings" > "SSH and GPG keys" > "New SSH key".
Here's how you can set up your SSH configuration to use a specific SSH key for a particular GitHub user:
-
Open or Create the SSH Config File: Open your SSH config file in a text editor. This file is usually located at
~/.ssh/config
. If it doesn't exist, you can create it.vi ~/.ssh/config
-
Add an Entry for the New SSH Key: Add an entry to the SSH config file for the new SSH key. You'll need to specify the host (which can be an alias), the user (which is
git
for GitHub), and the path to the private key file. Here's an example:Host github.com-newuser HostName github.com User git IdentityFile ~/.ssh/id_rsa_newuser
Replace
id_rsa_newuser
with the actual filename of your new SSH key. -
Update the Remote URL: Update the
origin
remote URL to use the alias you've defined in the SSH config file. You can do this with thegit remote set-url
command. For example:git remote set-url origin git@github.com-newuser:username/repo.git
Replace
username/repo.git
with the actual path to your repository. -
Test the Configuration: You can test the configuration by running
ssh -T git@github.com-newuser
. You should see a message indicating that you've successfully authenticated as the correct user.
By following these steps, you can ensure that the correct SSH key is used for the origin
remote of this specific project, allowing you to push to the repository with the correct user identity. This approach does not require changing the SSH configuration for your other projects.