sh script doesn't add ssh key to ssh-agent (windows git bash)

I am using Github for Windows on Windows 7. I have a bash script to add the ssh-key to my ssh-agent. I have setup a ssh remote repo.

add_key.sh

  • In Git for Windows Bash, how to install “get-apt” and “sudo” and all those basic commands?
  • Getting a list of all unversioned files in a Git-controlled folder
  • Bash and Git Flow bug in common file
  • Supplying a password to git in a bash script
  • In Git Bash on Windows 7, Colors display as code when running Cucumber or rspec
  • Modifying Jenkins Description for a build
  • #!/bin/bash    
    cd ../ssh/
    eval $(ssh-agent)
    ssh-add id.rsa
    cd ../htdocs/
    

    Execute command-

    ./add_key.sh
    

    It returns

    Agent pid 5548
    Identity added: id.rsa (id.rsa)
    

    When I git push origin master, it fails.
    But when I manually cd in the ssh directory, and run the same ssh-related commands and cd back to my directory htdocs and git push to origin master, it works.

    Why is this happening?

  • Arrow up and number on Eclipse meaning
  • Loading private repository submodules in Jenkins Project
  • How does Git's typical workflow compare to Mercurial's?
  • git clone is slow - can I just copy an existing clone to a new directory?
  • How to git stash pop specific stash in 1.8.3?
  • Capistrano deploy with bitbucket - Permission denied (publickey)
  • One Solution collect form web for “sh script doesn't add ssh key to ssh-agent (windows git bash)”

    Your problem is that your script is running in its own shell session because you are running ./add_key.sh.

    This means that the variables set by eval $(ssh-agent) are not living beyond that shell session so the parent session doesn’t have them and cannot use the agent (also you might be spawning a new agent each time you run the script).

    The fix for this is to run that “script” in the current session by dot-sourcing the script instead of running it as an external script.

    That is you want to use . add_key.sh.

    Git Baby is a git and github fan, let's start git clone.