Skip to content

Instantly share code, notes, and snippets.

@deekshithh
Created March 2, 2024 06:57
Show Gist options
  • Save deekshithh/7fe67f88eac8b95c1fa928b9ba063979 to your computer and use it in GitHub Desktop.
Save deekshithh/7fe67f88eac8b95c1fa928b9ba063979 to your computer and use it in GitHub Desktop.

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:

  1. 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).

  2. 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:

  1. 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
  2. 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.

  3. 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 the git 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.

  4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment