Home » SSH » How to Fix Git Not Using SSH Key in 4 Easy Ways?

How to Fix Git Not Using SSH Key in 4 Easy Ways?

Git Not Using SSH Key

You’ve generated an SSH key pair and plan to add the public key to GitHub, GitLab, or Bitbucket. Yet every time you run git push, the terminal asks for a username and password. Git seems to ignore your SSH key entirely.

In this article, I’ll walk you through four common reasons Git isn’t using your SSH key. Each method tackles a specific cause. You’ll also learn how to verify your setup so you can troubleshoot like a pro.

Why Git Not Using SSH Key

Git can authenticate via HTTPS or SSH. If the remote URL starts with https://, Git uses HTTPS and prompts for credentials. Even if the URL is SSH, Git relies on the underlying SSH client. That client must find a valid private key, loaded in an agent or specified in a config file. Common causes include:

  • File permissions are too loose, so SSH rejects the key.
  • You have multiple SSH keys, and the wrong one is offered.
  • Your SSH agent isn’t running.
  • The key wasn’t added back to the agent after a reboot.

These four scenarios cover 95% of cases and I’ll show you how to diagnose and fix each one.

Solution 1. How to Change Git Remote to SSH URL

The simplest culprit is the remote URL. When you clone a repo with the HTTPS link, Git stores that URL.

All further operations use HTTPS, ignoring your SSH key.

Therefore, you should check your current remote URL.

1. Open your terminal and navigate to your repository.

2. Run git remote -v. This command shows the fetch and push URLs.

3. If you see https://github.com/..., that’s the problem.

You should change the remote URL to SSH. To switch to SSH, copy the SSH URL from your Git hosting service. On GitHub, click Code, select SSH, and copy the link. On GitLab, Clone offers an SSH option. On Bitbucket, look for the Clone dropdown. Then paste the SSH URL you copied after git remote set-url origin. For example: git remote set-url origin git@github.com:username/repo.git. Pasting avoids typos and guarantees the correct format.

Verify with git remote -v again. Now try a git fetch. If it works without a password, you’ve solved it. If not, move to the next method.

Remember, you can also set the URL when cloning a new repo by using the SSH address directly. If you’re unsure which URL to use, check your hosting service’s documentation. For GitHub, the SSH URL format is always git@github.com:user/repo.git.

Method 2. How to Add SSH Key to SSH Agent?

Even with the correct SSH URL, Git won’t use a key that isn’t loaded into the SSH agent. The agent holds decrypted private keys in memory for the session. Many systems don’t automatically add keys on boot.

1. Check if the agent is running.

On Linux or macOS, run eval "$(ssh-agent -s)".

On Windows with Git Bash, the agent usually starts automatically, but you can verify with the same command.

2. List loaded keys

ssh-add -l

If you see key entries listed, the agent is already holding your keys. You can skip the next step and go straight to testing with ssh -T. If you see The agent has no identities, you need to add your key. Use ssh-add ~/.ssh/id_rsa. If your key has a different name, like id_ed25519, adjust the path. Enter your passphrase if prompted.

After adding, run ssh-add -l again to confirm. Now test the connection to GitHub with ssh -T git@github.com. You should see a success message with your username. If Git still asks for a password, the key might not match the public key on the server. Double-check that you uploaded the correct public key.

On some systems, you may need to add the key to your keychain: ssh-add --apple-use-keychain ~/.ssh/id_rsa on macOS, for example. If ssh-add complains about permissions, that’s a clue to check Method 4. The agent needs the key file to be secure.

Method 3. How to Specify SSH Key in SSH Config

Many developers work with multiple Git accounts. One for work and one for personal projects. Each account uses a different SSH key. Without explicit configuration, SSH may offer the first key it finds, which could be the wrong one. An SSH config file solves this.

Create or edit the file ~/.ssh/config. If it doesn’t exist, create it with a text editor. For each host, add a block like this:

Host github.com

HostName github.com

User git

IdentityFile ~/.ssh/id_rsa_personal

Replace id_rsa_personal with your key’s filename. You can add multiple hosts such as Host github.com-work with a different IdentityFile. Then, when cloning, use the host alias in the URL: git@github.com-work:user/repo.git.

After saving the config, set proper permissions: chmod 600 ~/.ssh/config. Test with ssh -T git@github.com. If you see the correct username, Git will now use that key. This method gives you fine-grained control and avoids agent confusion.

If you need to temporarily use a different key, you can also set the GIT_SSH_COMMAND environment variable: GIT_SSH_COMMAND='ssh -i ~/.ssh/other_key' git clone .... This is handy for quick tests without editing config files. Many GUI Git clients also let you specify SSH keys in their settings, but the config file works globally across all tools.

Method 4. How to Fix SSH Key File Permissions

SSH is paranoid about file permissions—and for good reason. If your private key file is readable by other users on the system, SSH assumes it’s compromised and refuses to use it. This often happens when you copy keys from a backup or create them with the wrong umask.

Navigate to your .ssh directory: cd ~/.ssh. List files with ls -l. Your private key should show -rw------- (600). If it shows -rw-r--r-- or worse, fix it with chmod 600 id_rsa. The public key can be 644.

The .ssh directory itself should be 700 drwx------. Run chmod 700 ~/.ssh if needed. You can also check the remote server’s authorized_key file, but local permissions usually fix the issue. After correcting permissions, test again with ssh -T git@github.com.

Many users are surprised how often this simple step resolves the issue. If you’re on Windows, permissions are less strict, but for WSL or Git Bash, these commands apply. For extra security, never share your private key. Keep it locked down. On Linux, you can also use `stat` to inspect permissions.

Git Not Using SSH Key FAQs

Q1: I added my SSH key to GitHub, but Git still asks for a password. Why?

Run git remote -v > If it shows https://, run git remote set-url origin git@github.com:user/repo.git

Q2: How can I tell which SSH key Git is offering to the server?

Run ssh -T git@github.com to see the authenticated username, or use GIT_SSH_COMMAND='ssh -v' git fetch for verbose output.

Q3: Can I use different SSH keys for two GitHub accounts on the same machine?

Edit ~/.ssh/config with a Host block and IdentityFile, then clone using the alias like git clone git@github.com-work:user/repo.git.

Conclusion

Git ignoring your SSH key is rarely a deep mystery. In most cases, the fix is a one-liner: change the URL, add the key to the agent, tweak the config, or lock down permissions. Work through these four methods, and you’ll push code securely in minutes. Bookmark this guide for next time.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *