'git push using ssh-agent in bash but not gui

Using git for windows (git-scm.com) and a script, i can get Bash to stop asking for the passphrase but i can not seem to remove the passphrase prompt from git GUI.

The url to the remote is using the ssh link (see below), so im pretty sure this is not the problem. But i am not an expert in git nor SSH..

[email protected]:repo

The SSH keys (ED25519) have been created with a passphrase provided and the public key was added to the hosting account. The SSH keys are stored in the typical location C:\Users\<user>\.ssh\id_ed25519. When clicking Help->Show SSH Key in the GUI, it shows my ssh key that is being used. The ssh-agent is running in the background and the ssh key has been added to the ssh-agent via the setup below.

Setting up the ssh-agent i had used the code provided here (copied and pasted below for reference) to create the .bashrc file under the C:\Users\<user> path. Everything seems to be running as expected under the bash prompt but not the Git GUI.

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

To me, it seems like the Git GUI is not using the ssh-agent when performing an push, fetch, or any other related remote feature. Is there a step i missed in setting up the ssh-agent, .bashrc script, and or the GUI to finish removing the passphrase prompt? or is there something else going on?

Any and all help is much appreciated, Thanks in advance



Solution 1:[1]

.bashrc runs when you start Git Bash. This affects the bash environment only but doesn't do anything for Windows as a whole. When you run the GUI, none of the setup in .bashrc is in effect. I use the Git integration in IntelliJ on my Windows laptop and still haven't figured out how to use my SSH keys with it. I currently open Git Bash to pull, push, or anything else that interacts with a remote.

Solution 2:[2]

Git GUI will look in your C:\Users\<User>\.ssh folder for a config file. If you add a config file (no extension) you can add the paths of your keys. Otherwise git GUI will only look for default key names. For some reason Git GUI doesn't execute ~/.profile or ~/.bashrc (Git Bash does), which can be used to autostart ssh agent and add keys.

This type of config file worked for me:

IdentitiesOnly=yes
PreferredAuthentications publickey
PasswordAuthentication no
IdentityFile C:\Users\<user>\.ssh\id_ed25519_your_key
IdentityFile /c/Users/<user>/.ssh/id_ed25519_your_key
IdentityFile ~/.ssh/id_ed25519_your_key

Any of the three path notations work. See https://linux.die.net/man/5/ssh_config for syntax of the config file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Code-Apprentice
Solution 2 elechris