Configure Git in PowerShell So You Don’t Have to Enter Your Password All the Damn Time

git, code 0 comments suggest edit

My last post covered how to improve your Git experience on Windows using PowerShell, Posh-Git, and PsGet. However, a commenter reminded me that a lot of folks don’t know how to get Git for Windows in the first place.

And once you do get Git set up, how do you avoid getting prompted all the time for your credentials when you push changes back to your repository (or pull from a private repository)?

I’ll answer both of those questions in this post.

Install msysgit

The first step is to install Git for Windows (aka msysgit). The full installer for msysgit 1.7.8 is here. For a detailed walkthrough of the setup steps, check out GItHub’s Windows Setup walkthrough. It’s pretty straightforward. That’ll put Git.exe in your path so that Posh-Git will work.

Bam! Done! On to the second question. Make sure you set up your SSH keys before moving to the second section.

Using SSH with Posh-Git

One annoyance with Git on Windows is when pushing changes to a repository (or pulling from a private repository), you have to constantly enter your password if you cloned the repository using HTTPS.

Likewise, if you clone with SSH, you also need to enter your passphrase each time. Fortunately, a little program called ssh-agent can securely save your pass phrase (and consequently your sanity) for the session and supply it when needed.

Update: Mike Chaliy just fixed PsGet so it always grabs the latest version of Posh-Git. If you installed Posh-Git before today using PsGet, you’ll need to update Posh-Git by running the following command:

Install-Module Posh-Git force

Unfortunately, at the time that I write this, the version of Posh-Git in PsGet does not support starting an SSH Agent. The good news is, the latest version of Posh-Git direct from their GitHub repository does support SSH Agent.

Since the previous step installed git.exe on my machine, all I needed to do to get the latest version of Posh-Git is to clone the repository.

git clone https://github.com/dahlbyk/posh-git.git

This creates a folder named “posh-git” in the directory where you ran the command. I then copied all the files in that folder into the place where PsGet installed posh-git. On my machine, that was:

C:\Users\Haacked\Documents\WindowsPowerShell\Modules\posh-git

When I restarted my PowerShell prompt, it told me it could not start SSH Agent.

powershell-ssh-agent-not-found

It turns out that it was not able to find the “ssh-agent.exe” executable. That file is located in C:\Program Files (x86)\Git\bin. but that folder isn’t automatically added to your PATH by msysgit.

If you don’t want to add this path to your system PATH, you can update your PowerShell profile script so it only applies to your PowerShell session. Here’s the change I made.

$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"

On my machine that script is at:

C:\Users\Haacked\Documents\WindowsPowerShell\Microsoft.Powershell_profile.ps1

The next time I opened my PowerShell prompt, I was greeted with a request for my pass phrase.

powershell-ssh-agent

After typing in my super secret pass phrase, once at the beginning of the session, I was set. I could clone some private repositories and push some changes without having to specify my pass phrase each time. Nice. Secure. Convenient.

The Start-SshAgent Command

The reason that I get the ssh-agent prompt when starting up PowerShell is because when I installed Posh-Git, it updated my profile to load in their example profile:

C:\Users\Haacked\Documents\WindowsPowerShell\Modules\posh-git\profile.example.ps1

That profile script is calling the Start-SshAgent command which is included with Posh-Git. If you don’t like their profile example, you can manually start ssh-agent by calling the Start-SshAgent command.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

39 responses

  1. Avatar for John Weldon
    John Weldon December 19th, 2011

    I had this in my profile for a while, but I ran into issues with poorly written third party scripts that tried to run my profile in non-interactive sessions, and they would hang on the prompt.
    I didn't find an easy way to detect a non-interactive session, so I just made a function in my profile that makes it easy to start ssh-agent, and add keys to it.
    https://gist.github.com/1499642 start-sshagent, addkey-sshagent, and helper functions

  2. Avatar for haacked
    haacked December 19th, 2011

    @John Weldon Posh-Git adds a function called Start-SshAgent too. You can call it as well.

  3. Avatar for John Weldon
    John Weldon December 19th, 2011

    Nice! I'll have to check it out.

  4. Avatar for Andres
    Andres December 19th, 2011

    I get the following error:
    "Could not open a connection to your authentication agent."
    Time to do some research.

  5. Avatar for Ken Egozi
    Ken Egozi December 19th, 2011

    I like PUTTY's PAGEANT for managing my SSH connection. It makes things easier especially when switching between hosted accounts that were set with different private keys.

  6. Avatar for Matt Honeycutt
    Matt Honeycutt December 19th, 2011

    Instead of installing msysgit, I would recommend installing Git Extensions. You will get msysgit plus a nice GUI, the diff tool of your choice, and a few other things.

  7. Avatar for Anton
    Anton December 19th, 2011

    Would be handful to be able to use Git right from Visual Studio via Package Manager Console shipped with NuGet since it's full-fledged PowerShell.

  8. Avatar for Fredrik Leijon
    Fredrik Leijon December 19th, 2011

    Or if you assume that your computer is secure you can generate a ssh key with a blank password to avoid entering you password on auth

  9. Avatar for Toni Parviainen
    Toni Parviainen December 19th, 2011

    For github keys I just leave the passpharase empty. I think it is pretty good compromise between security and usability. If someone would get hold of my key I can change it from the webpage thus blocking any future access.

  10. Avatar for Barry
    Barry December 19th, 2011

    $env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"
    is a bit more portable, I think.

  11. Avatar for Perry
    Perry December 19th, 2011

    Any help getting ssh-agent set up and installed? I have posh-git all set up and I can pull down from my private repository on Github, but I am still being prompted for my "super-secret phrase" on every command. And when I 'Start ssh-agent' a cmd prompt flashes but nothing really changes.

  12. Avatar for Bradley T. Landis
    Bradley T. Landis December 19th, 2011

    Perry,
    You should be running 'Start-sshagent' not 'Start ssh-agent'. That said, I see the ssh-agent process start and I still get prompted every time. I do have my machine set up with multiple keys and a config file in my .ssh directory. Does this only work in the simple case?
    Thanks,
    Bradley

  13. Avatar for Bradley T. Landis
    Bradley T. Landis December 19th, 2011

    I tried it on another machine that has ssh set up with only a single key pair and I got the same results. When I start powershell (while watching task manager's processes tab) I see ssh-agent start and I see ssh-add start and stop. I still get prompted for my passphrase on every git command though. If I run Start-sshagent myself, I get "ssh-agent is already started".

  14. Avatar for Bradley T. Landis
    Bradley T. Landis December 19th, 2011

    Okay, I think I figured it out. It doesn't work if your "Home" Directory path has spaces in it. Mine is "C:\Users\Bradley T. Landis" on both machines where I had problems.

  15. Avatar for Konstantin Tarkus
    Konstantin Tarkus December 19th, 2011

    I use Putty's SSH agent. Didn't know Git has its own..

  16. Avatar for Perry
    Perry December 19th, 2011

    The weird thing is that when I start it I see the sshadd start up in task manager but it never asks me password. And all of this happens really slowly...on the order of a minute or two.

  17. Avatar for Bradley T. Landis
    Bradley T. Landis December 19th, 2011

    Perry,
    Do you have spaces in the path to your ssh keys?
    If so you can use the following command:
    ssh-agent ssh-add "<FULL PATH TO KEY>"
    Thanks,
    Bradley

  18. Avatar for Perry
    Perry December 19th, 2011

    This continues to be really odd. Here is what I typed and responses
    >ssh-agent ssh-add C:\ptaylor\.ssh\id_rsa
    Enter passphrase for C:\users\ptaylor\.ssh\id_rsa: (typed pass)
    Identity Added: C:\Users\ptaylor\.ssh\id_rsa
    > git pull
    Enter passphrase for key '/c/Users/ptaylor/.ssh/id_rsa': Shouldn't it have remembered it now?

  19. Avatar for haacked
    haacked December 20th, 2011

    @Barry thanks! I fixed the post.

  20. Avatar for Bradley T. Landis
    Bradley T. Landis December 20th, 2011

    I fixed mine by replacing:
    & $sshAdd
    in the GitUtils.ps1 to:
    $sshPath = resolve-path ~/.ssh/id_rsa

    & $sshAdd $sshPath

  21. Avatar for Bradley T. Landis
    Bradley T. Landis December 20th, 2011

    Perry,
    If you watch the process list in task manager, I bet your ssh-agent is starting and then stopping right away because of the way your starting it. Try the edit to the powershell script I just posted.
    Thanks,
    Bradley

  22. Avatar for haacked
    haacked December 20th, 2011

    @Bradley I submitted it as a pull request. https://github.com/dahlbyk/posh-git/pull/25
    Next time, you should submit it as a pull request. You can do it all through the browser!
    I show an example here: haacked.com/...

  23. Avatar for Perry
    Perry December 20th, 2011

    Bradley-
    Your suggestion worked perfectly. Thanks for the help.

  24. Avatar for Minilening
    Minilening December 22nd, 2011

    Thanks very much Bradley. You helped me out here!

  25. Avatar for john
    john December 25th, 2011

    nice...

  26. Avatar for calenti
    calenti December 29th, 2011

    This is a great tutorial and a good intro to PowerShell. I did notice that this works OK on initial start, but if I close the PowerShell the ssh-agent doesn't terminate, and when I start the PowerShell again it doesn't seem able to communicate with the ssh-agent.
    For me, killing the old ssh-agent and running start-sshagent does the trick. Nice tip and it got me off the gate in getting into Powershell. Thanks.

  27. Avatar for Barry
    Barry January 5th, 2012

    Footnote for anyone that is interested: for reasons I haven't bothered delving into yet, if your profile loads posh-git AND posh-hg, setting the PATH somehow fails and your powershell might not start.

  28. Avatar for Afvallen zonder dieet
    Afvallen zonder dieet January 20th, 2012

    Nice tutorial man.. Please keep up the good work!!

  29. Avatar for Restuta
    Restuta June 5th, 2012

    I guess your
    "Install-Module Posh-Git –force"
    should be replaced with Import-Module. I am unable to find "Install-Module" cmdlet.

  30. Avatar for Jonas
    Jonas June 6th, 2012

    I'm having problems entering my password when PowerShell asks for it:
    Enter passphrase for C:\Users\jonas\.ssh\id_rsa:
    I tried entering it manually, and I also tried copying it to the clipboard and then choosing 'Edit' --> 'Paste' in Powershell. It always responds with
    Bad passphrase, try again ...
    The password I'm entering is correct, it is the one I noted when I created the SSH key, and it works great with Pageant/Putty and TortoiseGit.
    Thanks for any hints.

  31. Avatar for yggdrasil
    yggdrasil July 18th, 2012

    @Restuta The Install-Module command is provided by the PsGet tool, which makes it easy to install third-party PS modules. See http://psget.net/ for more info.

  32. Avatar for Thomas Ferris Nicolaisen
    Thomas Ferris Nicolaisen August 14th, 2012

    I've got a little write-up on how to set up Puttygen/pageant/plink here, if anyone's interested:
    blog.tfnico.com/.../my-git-setup-on-windows.html

  33. Avatar for Edward
    Edward December 3rd, 2012

    +1 to @Matt --- installing the extensions has been the smoothest --- I've never had any problems with ssh-agent ...

  34. Avatar for leventov
    leventov May 17th, 2014

    God bless you.

  35. Avatar for ngdelcastillo
    ngdelcastillo November 15th, 2014

    Thanks! This is the missing link in my powershell config.

  36. Avatar for hsirah
    hsirah August 19th, 2015

    I realize this is an old post. However, this helped me a lot when I started using Git with PowerShell.

    With the release of Git for Windows 2.5, the "ssh-add.exe" (and other such exe's) have been moved to C:\Program Files (x86)\Git\usr\bin. Hope this helps someone.

  37. Avatar for Bernard Chen
    Bernard Chen August 31st, 2015

    I encountered this as well, but in addition to using \usr\bin instead of \bin, I also needed to use c:\Program Files\ instead of c:\Program Files (x86)\. When I upgraded git through chocolatey, the 64-bit version was installed instead of the 32-bit version.

  38. Avatar for raeesiqbal
    raeesiqbal April 4th, 2017

    I've created a tool called "ssh-agent-helper" that invokes `ssh-agent` and makes it available globally, so that, you can use `ssh-add`, `ssh` or `git` from any terminal e.g. CMD, Bash, PowerShell etc without any configuration.

    You can find it here: https://github.com/raeesbha...

  39. Avatar for Steven Stark
    Steven Stark September 18th, 2017

    I found an issue with this approach. When running ssh-agent it kicks me out of PowerShell ( I am assuming ). Look at this output, and notice the lack of 'PS' in the final terminal line.

    Windows PowerShell
    Copyright (C) 2016 Microsoft Corporation. All rights reserved.

    PS H:\> start-ssh-agent
    Found ssh-agent at 1620
    Found ssh-agent socket at /tmp/ssh-0uoLKZ4BFotl/agent.13264
    Microsoft Windows [Version 10.0.14393]
    (c) 2016 Microsoft Corporation. All rights reserved.

    H:\>