Problem
Git operations fail with the below error message. After migrating the projects to a new laptop, there are issues caused by IDE retaining the old SSH user path for the git client.
The message names a private key file that does not exist, for example:
no such identity: /Users/<old-user>/.ssh/<key>_github: No such file or directory
``
Cause
The IDE runs your Git executable, and Git runs ssh. ssh shows "no such identity" only for a key path that is set explicitly. It does not show it for the default keys (~/.ssh/id_ed25519, ~/.ssh/id_rsa and others). The explicit path can come from one of these places:
- an
IdentityFileline in the SSH config file (~/.ssh/config, orC:\Users\<user>\.ssh\configon Windows) - the Git setting
core.sshCommand, for examplessh -i <path> - the environment variable
GIT_SSH_COMMAND
After a migration, this path often contains the old user name or the old home directory, or the key file was not copied to the new machine.
Resolution
Find the key path
Run these commands in a terminal in the project directory. The commands work on macOS, Linux, and Windows (Git Bash or PowerShell).
- Show the key paths that ssh uses for your Git host. Replace github.com with your host:
ssh -G github.com | grep -i "^identityfile"In PowerShell, usessh -G github.com | Select-String "^identityfile". - Show
core.sshCommandand the file that sets it:git config --show-origin --get-all core.sshCommand - Show GIT_SSH_COMMAND. In a macOS or Linux terminal, run
echo "$GIT_SSH_COMMAND". In PowerShell, runecho $env:GIT_SSH_COMMAND.
Look for the path from the error message.
Fix the key path
- If the key file exists on the new machine, change the path to it. Edit the
IdentityFileline, thecore.sshCommandvalue, or theGIT_SSH_COMMANDvariable that you found above. In the SSH config file, write the path with ~, for example IdentityFile~/.ssh/<key>_github. A path with ~ stays correct when the user name changes. - If the key file does not exist, copy it from the old machine to
~/.ssh/. On macOS and Linux, run chmod 600 ~/.ssh/ after you copy it. You can also create a new key and add its public key to your Git hosting account. - Test the connection, for example
ssh -T git@github.com. - If you set
GIT_SSH_COMMANDor changed an environment variable, restart the IDE. The IDE reads environment variables only at startup. - Repeat the Git operation in the IDE.
Start the SSH agent (optional)
Windows
-
Ensure that you are running your local PowerShell as an administrator.
-
Enter the following commands:
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
Linux
- Start the SSH agent with the following command:
eval "$(ssh-agent -s)"
- Add the following lines to
~/.bash_profileor~/.zprofileif you prefer to use thezshshell instead ofbash.
if [ -z "$SSH_AUTH_SOCK" ]; then
# Check for a currently running instance of the agent
RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
if [ "$RUNNING_AGENT" = "0" ]; then
# Launch a new instance of the agent
ssh-agent -s > $HOME/.ssh/ssh-agent
fi
eval `cat $HOME/.ssh/ssh-agent` > /dev/null
ssh-add $HOME/.ssh/<your ssh key> 2> /dev/null
fi
- On the last line of the suggested code, replace with your specific ssh key. For example,
ssh-add $HOME/.ssh/id_ed344 2> /dev/null
macOS
macOS starts the SSH agent automatically. Add your key to the agent and store its passphrase in the keychain:
ssh-add --apple-use-keychain ~/.ssh/<your ssh key>
To load the key after a restart, add these lines to ~/.ssh/config:
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/<your ssh key>
Comments
0 comments
Please sign in to leave a comment.