# Configuring SSH Keys for Individual Git Commands

tags
  #Git #Ssh
published
  October 2, 2024
reading time
  3 minutes

When working with Git repositories, you often need to authenticate using SSH keys. While you can configure SSH settings globally, there are times when you need to use a specific SSH key for just one command. This is particularly useful when cloning a new repository or adding a submodule, where there isn’t yet a local `.git/config` file to modify.

## The Problem

Imagine you’re working on a project that requires you to clone a repository using a specific SSH key. Normally, you might edit your SSH config file or the repository’s `.git/config` file. But what if you’re just getting started and don’t have these files set up yet?

## The Solution

Git provides a handy way to set configuration options for a single command using the `-c` flag. This allows you to specify the SSH command to use, including the path to your private key file.

Here’s the syntax:

    git -c core.sshCommand="ssh -i /path/to/private_key_file" <git command>

## Examples

### Cloning a Repository

To clone a repository using a specific SSH key:

    git -c core.sshCommand="ssh -i /path/to/private_key_file" clone git@github.com:username/repo.git

### Adding a Submodule

Similarly, when adding a submodule:

    git -c core.sshCommand="ssh -i /path/to/private_key_file" submodule add git@github.com:username/submodule.git

## Making SSH Keys Available

Before using SSH keys with Git, it’s important to check which keys are already available on your system:

1. Check Available Keys: List the contents of your SSH directory:

       ls -al ~/.ssh

   This command will show you all files in your SSH directory, including your key files (typically with extensions like .pub for public keys).

2. Common Key Names: Look for files like:

   - id_rsa, id_rsa.pub
   - id_ed25519, id_ed25519.pub
   - id_ecdsa, id_ecdsa.pub

3. Add to SSH Agent: If you find the key you want to use, ensure it’s added to your SSH agent:

       eval "$(ssh-agent -s)"
       ssh-add ~/.ssh/your_private_key_file

4. Verify Key in Git Host: Make sure the corresponding public key (the .pub file) is added to your Git host (e.g., GitHub, GitLab) in the SSH keys section of your account settings.

If you don’t find any suitable keys, you may need to generate a new one. However, for the purposes of this guide, we’re assuming you’re working with existing keys.

## Adding sshCommand to .git/config

After cloning a repository using the method described above, you might want to make this configuration permanent for the specific repository. Here’s how to add the sshCommand to your local `.git/config` file:

1. Navigate to your repository’s root directory.

2. Open the `.git/config` file in a text editor.

3. Add the following lines under the `[core]` section (or create it if it doesn’t exist):

       [core]
           sshCommand = ssh -i /path/to/private_key_file

4. Save and close the file.

Now, all Git commands for this repository will use the specified SSH key without needing the `-c` flag.

## Why This Matters

This technique is invaluable in several scenarios:

1. Multiple SSH Keys: If you work with different Git hosts or have multiple accounts on the same host, you may need to use different SSH keys for different repositories.

2. Temporary Access: When you need to grant someone temporary access to clone a repo without modifying global SSH settings.

3. Automation: In CI/CD pipelines or scripts where you need to clone repositories with different authentication methods.

4. Testing: When troubleshooting SSH issues, this allows you to test different keys without changing your global configuration.

## Conclusion

Using the `-c` flag with Git commands provides a flexible way to configure SSH settings on a per-command basis. This is especially useful when working with new repositories or in environments where you can’t or don’t want to modify global configurations. By mastering this technique, you can streamline your Git workflows and handle complex authentication scenarios with ease.

Remember, while the `-c` flag method is convenient for one-off commands, for long-term projects, it’s usually better to set up your SSH config file or the repository’s `.git/config` for a more permanent solution.