Sourcetree 4.0.1 SSH key selection – how to explicitly choose a key?
0 reputation · 01 Dec 2021, 19:21 UTC
0 reputation · 01 Dec 2021, 19:21 UTC
Ensure that Sourcetree 4.0.1 uses a specific SSH key for Git operations when more than one key is available in the system’s SSH agent.
The application does not expose a UI to choose the key, and the key used appears to be the first returned by the agent or the default key. This can cause failed pushes or pulls in environments with multiple identities.
~/.ssh/config or agent forwarding) that reliably force the intended key without external scripts?No. Sourcetree 4.0.1 does not expose a per-remote SSH key picker, and it does not let you choose which key the SSH agent offers. The key used is determined by the SSH client backend configured in Sourcetree and by that backend's agent or configuration.
When multiple identities are loaded, the SSH client typically offers the first key returned by the agent or the default key. Sourcetree does not override this ordering, so failed pushes or pulls can occur when the wrong identity is presented to the remote server.
~/.ssh/config can restrict which key is offered for a given host using IdentityFile and IdentitiesOnly yes.Which SSH client backend is selected in Sourcetree: OpenSSH or PuTTY/Pageant? This determines whether to edit ~/.ssh/config or manage keys in Pageant.
~/.ssh/config, creating it if necessary, and add a host block for the remote:Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
git@github-work:owner/repo.git.ssh-add ~/.ssh/id_ed25519_work.ssh -vT git@github-work. Confirm that only the intended key is offered and accepted.IdentitiesOnly yes prevents the client from offering every loaded key. This fails if the identity file is missing, unreadable, or encrypted and not loaded in the agent.
Sourcetree 4.0.1 menus and SSH backend support vary by edition and operating system. Verify the installed build and the selected SSH client before applying these steps. This is a conservative draft for human review; confirm behavior in your environment.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 02 Dec 2021, 02:10 UTC
Setting IdentityFile in ~/.ssh/config does not by itself stop the SSH agent from offering other loaded keys. Without IdentitiesOnly yes, the client may attempt several identities before the intended one, and Git hosts that limit authentication attempts can drop the connection first. On Windows, confirm Sourcetree is using OpenSSH in Tools > Options > General > SSH client; the config file is typically %USERPROFILE%\.ssh\config. After adding the host block, update the remote with git remote set-url origin git@alias:owner/repo.git. Verify with ssh -Tv git@alias: successful selection should show only the intended key in the Offering public key debug lines.