Skip to content

Instantly share code, notes, and snippets.

@dmangiarelli
Last active March 22, 2023 22:20
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save dmangiarelli/1a0ae107aaa5c478c51e to your computer and use it in GitHub Desktop.
Save dmangiarelli/1a0ae107aaa5c478c51e to your computer and use it in GitHub Desktop.
How to setup SSH with Git for Windows

How to setup Git for Windows

I know this document seems long, but it shouldn't be too difficult to follow. This guide is based on Windows, but every program here has Linux/Mac equivalents, and in most cases they're built-in. So, take a deep breath and go step by step.

The steps below are for GitHub, but the steps are almost idential for Bitbucket, Heroku, etc.

You'll probably want to make sure Chocolatey is installed, since it streamlines installing this stuff later. If you install via Chocolatey, you don't need to run the installers from the products' respective sites.

Cmder / ConEmu

The first thing you'll want to do is to download and install Cmder, which is a terminal program that serves as excellent replacement for the built-in cmd.exe. It's not a shell itself, so it supports running plain old cmd.exe commands and running PowerShell.

As a comment notes below, Cmder is actually a packaged version of ConEmu. If you don't want the out-of-the-box configuration that Cmder comes with, you can install ConEmu by iteslf and customize it to your needs from there. Both of course have the same features though.

With Chocolatey, run one of the following from an elevated propt:

choco install cmder

or

choco install conemu

Git

Git uses a per-user config file located at %USERPROFILE%\.gitconfig. It's a plain-text file you can edit with your favorite text editor (or Notepad, if you haven't chosen a favorite yet). Note that git calls this config the "global" config. It's more general than per-repository config, and more specific than the machine config.

TortoiseGit

This is optional, but highly recommended. Download and install TortoiseGit. You'll want some of the tools it installs later. If you have another Tortoise installed (e.g. TortoiseSVN) and you don't want to have TortoiseGit's context menu clutter, you don't have to download it. If you're not sure, get it. TortoiseGit is nice because it adds overlay icons (that don't always update properly). Another benefit is that a full PuTTY install, which you'll also need.

If you really don't want the extra shell extension (I don't blame you), you can install TortoiseGit, copy TortoiseGitPlink.exe from TortoiseGit's bin/ directory, store it somewhere else, and then uninstall TortoiseGit. Then, later on when you set the GIT_SSH environment variable, just use the new path to it.

Chocolatey:

choco install tortoisegit

PuTTY

You can skip this step if you installed TortoiseGit.

If you didn't install it, download and run the Windows Installer so you get all the apps installed from one package.

Chocolatey:

choco install putty

Authentication

There are a few different ways to authenticate with GitHub. The easiest is to use Git Credential Manager for Windows. It supports authenticating with GitHub over HTTPS even with two factor authentication. If you use this, you can skip to the "Install Git" section. If you wan to use SSH, read on.

SSH Setup with PuTTY

If you're using a service like GitHub or Bitbucket, you have a couple of options when authenticating so you can push your code. This will allow you to share your code with other people. Even if you're the only person working on a project, those sites can serve as a backup.

Set up SSH keys on Windows:
  1. Open PuTTYgen by searching for it in the Start menu or Start screen. If you see multiple entries, any will work.
    Leave the settings as they are, unless you know what you're doing.
  2. Click "Generate".
  3. Wiggle the mouse around in the top part of the window until the progress bar is full, as the program asks you to do.
  4. Once you've provided enough entropy, a bunch of text fields will appear. It's highly recommended that you provide a passphrase.
  5. After providing a passphrase, click "Save private key". The usual directory to save these in is %USERPROFILE%\_ssh.
    It doesn't matter what you call the key, but for demonstration purposes, I'm going to call it github.ppk. This file should have an extension of .ppk.
  6. Now, log in to GitHub.com. Don't close PuTTYgen yet.
  7. Go to your Account settings and then to SSH keys.
  8. Click "Add SSH key". Copy the text in the top text box in PuTTYgen, the one labeled "Public key for pasting into OpenSSH authorized_keys file" and paste it into the Key box in GitHub. Give it a title that describes what machine the key is on (e.g. "Work laptop").
  9. Click "Add key".

And with that, we're done setting things up to connect to GitHub.

If you also want a native command-line build of ssh (i.e., ssh.exe or ssh-agent.exe) to work, you'll need to also export your key in OpenSSH format. You can do this from PuTTYgen by clicking on Conversions > Export OpenSSH Key. These keys are typically saved in %USERPROFILE%\.ssh or (~/.ssh in *nix-style paths, which also work in Bash environments on Windows). Once you export the key, you should copy it to %USERPROFILE%\.ssh\id_rsa. See the comments for some more information. Source

Pageant

Now your SSH keys are set up and you can use them to push to and pull from GitHub, but you still need to do one more thing to use those keys. Before you want to push code to GitHub, you'll need to do this, but once you do it, you won't have to do it again until you restart your computer.

  1. Run Pageant (search for it in the Start menu/screen).
  2. Right-click the Notification Area icon (it looks like a PC with a dark sombrero).
  3. Click "Add Key".
  4. Find the .ppk file you created above.
  5. Enter the passphrase you used before.
    If the passphrase prompt goes away, you should be all set.
    If you want to confirm, right-click on the Pageant icon again and click on "View Keys".

Now, you can pull from and push to GitHub without being pestered for your password, at least until you log off. You can also double-click on the notification area icon to see what keys have already been added to Pageant.

Install Git

Git for Windows

Finally, we get to the part where we get to install Git.

  1. Download the Git installer.
    Or, with Chocolatey (which may be a patch or two behind the main site, and will always install the shell extension).
    choco install git
  2. When installing, pick the following options:
  3. Uncheck the box for Windows Explorer integration.
  4. Choose "Run Git from the Windows Command Prompt".
  5. Choose "Checkout Windows-style, commit Unix-style line endings".
  6. Let the installer finish.

You may need to manually add a System Environment Variable (Start > Search for "Edit the system environment variables"). Add a new System (not User) variable. Give it the name GIT_SSH, and a value of the path to a TortoiseGitPlink.exe (e.g. C:\Program Files\TortoiseGit\bin\TortoisePLink.exe).
Or, the PowerShell way (run from an elevated PowerShell prompt): [Environment]::SetEnvironmentVariable("GIT_SSH", "C:\Program Files\TortoiseGit\bin\TortoiseGitPLink.exe", "Machine")

posh-git

This is mostly optional if you're using Cmder, but if you want more general support for Git in PowerShell, you can install an awesome package called posh-git. Because PowerShell is awesome, and you should be using it instead of batch scripts and plain old cmd.exe as much as you can. Even without this, you can use Git commands from PowerShell, but posh-git will give you status information right in the prompt.

  1. Make sure you have PowerShell 5 or later installed.
    To check, open PowerShell from your Start menu/screen and type $PSVersionTable. The PSVersion value should be 5.0 or greater.
    If you don't have v5 or later installed, download and run the correct installer for Windows Management Framework 5 (which comes with PowerShell 5) from this page.
  2. Change PowerShell's script execution policy.
    Open an elevated PowerShell prompt and enter:
    Set-ExecutionPolicy RemoteSigned
  3. Install Posh-Git.
    Install-Module Posh-Git

Now, whenever you're in a Git workspace directory in your PowerShell prompt, you'll get a fancy prompt, and you can still use tab completion and standard Windows paths. Hooray! Git will still echo paths with backslashes, but it will recognize forward slashes.

Other Tools

  • Git Extensions is another shell extension that also has a Visual Studio extension. It's very user-friendly.
  • GitHub Desktop, not to be confused with Git for Windows, is GitHub's Windows Git client.
  • SourceTree is to Bitbucket as GitHub for Windows is to GitHub: it's Atlassian's answer to GitHub for Windows.

Config Tweaks

A difftool is installed by default with TortoiseGit, but I'm a fan of using SourceGear DiffMerge for diffs and P4Merge for merging. These may be installed via Chocolatey with choco install diffmerge p4merge. Install those programs and add the following to your .gitconfig file:

[diff]
    tool = sgdm
    guitool = sgdm
[difftool]
    prompt = false
[difftool "sgdm"]
    cmd = "C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe" --nosplash "\"$LOCAL\"" "\"$REMOTE\""
    path = "C:/Program\\ Files/SourceGear/Common/DiffMerge/"
[difftool "p4merge"]
    cmd = p4merge.exe\"$LOCAL\"\"$REMOTE\"
[merge]
    tool = p4merge
[mergetool]
    keepBackup = false
    prompt = false
[mergetool "sgdm"]
    cmd = "C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe" --nosplash --merge --result="\"$PWD/$MERGED\"" "\"$PWD/$LOCAL\"" "\"$PWD/$BASE\"" 
    trustExitCode = true
    prompt = false
[mergetool "p4merge"]
    cmd = "p4merge.exe" "\"$BASE\"" "\"$LOCAL\"" "\"$REMOTE\"" "\"$MERGED\""
    keepTemporaries = false
    trustExitCode = true
@NikolayIT
Copy link

Great explanation! Thanks!

@docrinehart
Copy link

Just throwing this out there, but Cmder is just a wrapper around ConEmu. FYI. To be sure though, it has a lot of great extra features, and I use it daily. ^_^ Also, it has a new home (the link above is dead): http://cmder.net/

@Miroufff
Copy link

Miroufff commented Jun 3, 2016

Thank's!!!!!

@CarlaMck77
Copy link

Thank you for this article. I have an issue when I try to pull a file from bitbucket. it says 'Permission denied (publickey)'. I used the ppk as prescribed. Am I missing something?

@CarlaMck77
Copy link

CarlaMck77 commented Jun 17, 2017

@dmangiarelli
Copy link
Author

@Carlitta87, I think the reason you had that issue was because you were using ssh.exe instead of PuTTY. The process is similar, but as you saw you also need to export an OpenSSH-format key. I added a note about that.

Notifications don't work on gist comments so I'll won't see any comments unless I manually check, sorry about that.

@dotnetchris
Copy link

FYI i sliced off a fork that i've been targetting more to how i specifically use git & tools but incase you want to merge anything i added or if it's useful to anyone else: https://gist.github.com/dotnetchris/a40e02f98754a8cacd4f3b8679ce159d

@Chaguala
Copy link

Chaguala commented Jun 7, 2018

I love it

@knocte
Copy link

knocte commented Aug 17, 2018

FYI I combined this guide with another one (best of both worlds) and placed it here: https://serverfault.com/a/850156/137305

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment