git is a powerful version control system that allows developers to manage changes to their source code over time. One of its key features is the ability to use SSH keys for authentication, enhancing security by eliminating the need to enter a password each time. However, git doesn't automatically know which SSH key to use. In this guide, we'll walk you through the steps to tell git which SSH key to use.

Before we dive in, ensure you have an SSH key pair generated. If not, you can create one using the following command in your terminal:

``` ssh-keygen -t ed25519 -C "your_email@example.com" ```
Once your key pair is generated, it's time to tell git which one to use.
Setting Up Your SSH Key

Before git can use your SSH key, you need to add it to the SSH agent, which manages your keys.
The process varies slightly depending on your operating system. Here, we'll cover the most common scenarios: macOS and Linux, and Windows users.

macOS and Linux
Open your terminal and run the following command to add your SSH key to the SSH agent:
``` eval "$(ssh-agent -s)" ```
Then, add your SSH private key to the agent:

``` ssh-add ~/.ssh/id_ed25519 ```
Replace .ssh/id_ed25519 with the path to your private key if it's different.
Windows
On Windows, you'll use PuTTY's pageant tool to manage your SSH keys. Run pageant, add your private key (.ppk file), and then start Git Bash or your preferred terminal.

Now, let's tell git which SSH key to use.
Configuring git








git uses a global settings file, .gitconfig, to store its user and repository settings. We'll use this file to tell git which SSH key to use.
Open your terminal and run the following command to edit your git config file:
``` git config -e -g ```
Add the following lines to the file, replacing ~/.ssh/id_ed25519 with the path to your private key:
``` git fetch = "!f() { git fetch --all --prune; }" git remote.set-url = "f() { if [ \"$1\" = \"origin\" ] && [ -n \"\${SSH_AGENT_PID}\" ]; then echo \"git@github.com:USER/REPO.git\"; else}""' ```
In the second line, replace USER and REPO with your GitHub username and repository name.
After making these changes, save and close the file. Now, git will use your specified SSH key for any repository with the origin remote.
There you have it! You've successfully told git which SSH key to use. The next time you push or pull from your repositories, git will use your specified SSH key for authentication.
Remember, it's essential to keep your SSH keys secure. Never share your private key, and consider adding a passphrase for an extra layer of security. Regularly updating your SSH keys and removing inactive ones is also a good practice.
Happy coding! Now that you've got git set up with your SSH key, you can focus on what you do best: writing amazing code. If you found this guide helpful, share it with your fellow developers, and subscribe to our newsletter for more tips and tricks on mastering git.