git push using ssh-agent in bash but not gui
Asked Answered
R

3

6

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

Receiptor answered 14/6, 2019 at 20:55 Comment(4)
Which Git GUI? What command do you type to launch that Git GUI? Do you type from the bash session?Dialogue
Using git for windows (git-scm.com). Typically I launch the GUI from the context window (right clicking the folder in the windows explorer). Then doing the typical usage of staging, commit, and or push throughout the work flow. And then closing the window until the next time.Receiptor
@VonC, this does bring up good points. It is interesting to note, that if git GUI is launched from the bash using git gui command, git GUI stops asking for the ssh passphrase...Receiptor
Yes, from the context menu, it could use "All Users" or "System" account, which does not have a ~/.ssh folder as your own C:\Users\<user> does.Dialogue
R
2

Viewing a Guide i found here, this led me to search things differently. Ultimately telling me to use Putty and its ssh agent to manage passwords for me.

Eventually stumbled upon this post in which they talk about how to get git to use putty's ssh agent. This simply came down to me putting GIT_SSH into the environment variables pointing to plink.exe, found within putty's installation folder.

When using this method, it does present an error that i'm not sure is just my machine/installation or if this effects everyone. The problem was solved here in this post by opening putty and ssh into the host to generate the cache this way. Because there is already a posting on this, i'll refrain from going any further into why git could not do this step for me.

Also note from my above question, i was using a .bashrc script to automatically launch keys into the OpenSSH ssh-agent upon opening git bash. Because of this putty approach, this script and all the relevant files were removed. This short list of removed files were the agent.env that the script creates, the .bashrc that held the script, and also the .pash_profile that was created.

Once done, both git bash and git gui were successfully using putty agent and can be used anyway i like. I can even launch git gui from the context menu and have it recognize the ssh keys and no longer prompt for a passphrases.

Receiptor answered 11/7, 2019 at 19:23 Comment(1)
I'm glad you found a solution. And thanks for writing it up. Hopefully this will be useful for others in the future.Cooper
C
2

.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.

Cooper answered 18/6, 2019 at 14:55 Comment(4)
While this is half expected and ok(because i can run the bash script on startup/login), is there any way to get git GUI to recognize the ssh-agent that is currently running? As resolving this is the basis of my question.Receiptor
"is there any way to get git GUI to recognize the ssh-agent that is currently running?" I assume you mean the ssh-agent that you start in .bashrc when you run Git Bash. Is that correct? If so, no, that ssh-agent is local to Git Bash. You will need to find a way to run the ssh-agent in the outer environment so that it is available to all applications that you run in Windows.Cooper
AH! I was unaware of this. Because of this, is there an easier alternative setup to get the git GUI to stop asking for the passphrase? i realize this is a new question, but there is relevance in answering the question and updating it to provide a link for others that hit this too..Receiptor
@Receiptor This is what I was trying to explain from the start. I think the wording in my comment is probably more clear than that in my original answer. I don't know of any solution off the top of my head. I primarily work in Linux and only code in Windows occasionally.Cooper
R
2

Viewing a Guide i found here, this led me to search things differently. Ultimately telling me to use Putty and its ssh agent to manage passwords for me.

Eventually stumbled upon this post in which they talk about how to get git to use putty's ssh agent. This simply came down to me putting GIT_SSH into the environment variables pointing to plink.exe, found within putty's installation folder.

When using this method, it does present an error that i'm not sure is just my machine/installation or if this effects everyone. The problem was solved here in this post by opening putty and ssh into the host to generate the cache this way. Because there is already a posting on this, i'll refrain from going any further into why git could not do this step for me.

Also note from my above question, i was using a .bashrc script to automatically launch keys into the OpenSSH ssh-agent upon opening git bash. Because of this putty approach, this script and all the relevant files were removed. This short list of removed files were the agent.env that the script creates, the .bashrc that held the script, and also the .pash_profile that was created.

Once done, both git bash and git gui were successfully using putty agent and can be used anyway i like. I can even launch git gui from the context menu and have it recognize the ssh keys and no longer prompt for a passphrases.

Receiptor answered 11/7, 2019 at 19:23 Comment(1)
I'm glad you found a solution. And thanks for writing it up. Hopefully this will be useful for others in the future.Cooper
I
0

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.

Idette answered 17/5, 2022 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.