13

This is what I did:

cd ~
touch .bashrc
notepad .bashrc

and the content of my .bashrc is (found on the web somewhere):

SSH_ENV="$HOME/.ssh/environment"

# start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."
    # spawn ssh-agent
    ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
    echo succeeded
    chmod 600 "$SSH_ENV"
    . "$SSH_ENV" > /dev/null
    ssh-add
}

# test for identities
function test_identities {
    # test whether standard identities have been added to the agent already
    ssh-add -l | grep "The agent has no identities" > /dev/null
    if [ $? -eq 0 ]; then
        ssh-add
        # $SSH_AUTH_SOCK broken so we start a new proper agent
        if [ $? -eq 2 ];then
            start_agent
        fi
    fi
}

# check for running ssh-agent with proper $SSH_AGENT_PID
if [ -n "$SSH_AGENT_PID" ]; then
    ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
    test_identities
    fi
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
else
    if [ -f "$SSH_ENV" ]; then
    . "$SSH_ENV" > /dev/null
    fi
    ps -ef | grep "$SSH_AGENT_PID" | grep -v grep | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
        test_identities
    else
        start_agent
    fi
fi

Somehow that script is not executed at all. I don't see any of the strings that should be echoed. I am familiar with Unix commandline in Linux and Mac OS X, but I have no idea how it works under Windows. Any suggestions please?

EDIT: Okay, my mistake... this script is executed, but I don't fully understand what it does. I was hoping to prevent being asked for passphrase every time I push to remote repo. As it stands now I'm still asked every time.

codedog
  • 2,488
  • 9
  • 38
  • 67
  • This `.bashrc` is totally UNIX-only. What are you trying to achieve with having it in your git bash? – favoretti Aug 11 '12 at 23:25
  • I mentioned my reasoning for this at the end of my question after I edited my question. How can I achieve this? I don't have a full understanding of how SSH works. – codedog Aug 11 '12 at 23:36

3 Answers3

20

Bingo! Something was obviously wrong with the way the ssh-agent is run in that .bashrc. I copied the one from here and it works a treat! Now I only have to enter my passphrase once when git bash starts up, and any subsequent push no longer need it.

Here's the actual content of the script now:

SSH_ENV=$HOME/.ssh/environment

function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
     echo succeeded
     chmod 600 ${SSH_ENV}
     . ${SSH_ENV} > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     #ps ${SSH_AGENT_PID} doesn't work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi
codedog
  • 2,488
  • 9
  • 38
  • 67
  • 1
    Now that I got this to work I found the best command line solution: Powershell + Console2 + Posh-git! – codedog Aug 14 '12 at 06:48
  • +1 TY ... [GitHub's Example](https://help.github.com/articles/working-with-ssh-key-passphrases) didn't work for me either :: this one did – Edward J Beckett Feb 13 '13 at 09:15
  • In my case, I had to surround all ${...} with double quotes (eg : "${SSH_ENV}" etc) – Olivier J. Apr 08 '13 at 19:59
  • I had to put $HOME in " because of spaces in the windows user name ```SSH_ENV="$HOME"/.ssh/environment``` – powtac Aug 28 '14 at 16:03
8

The simplest mechanism on Windows is the one from here http://anterence.blogspot.co.uk/2012/01/ssh-agent-in-msysgit.html

Modified to show key names other than 'id_rsa', the .bashrc file looks like this:

#! /bin/bash 
eval `ssh-agent -s` 
ssh-add ~/.ssh/github_rsa
ssh-add ~/.ssh/otherkey_rsa

You are prompted for the passphrase for each key in turn, but only the first time after a machine start.

Matthew Skelton
  • 2,220
  • 21
  • 21
0

Try this, works for me.

SSH_ENV=$HOME/.ssh/environment

function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
     echo succeeded
     chmod 600 ${SSH_ENV}
     . ${SSH_ENV} > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     #ps ${SSH_AGENT_PID} doesn't work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi
rizon
  • 8,127
  • 1
  • 27
  • 17