Git in PowerShell on Windows: Installing Git, Generating an SSH Key, Adding It to your SSH-Agent, and Using GitHub
I’ve been a long-time fan of Linux and MacOS for development, but I recently set a challenge for myself to set up my development environment in Windows PowerShell. One part of that is working with Git and repositories in GitHub.
Step 1: Install and configure OpenSSH
- Click the Start menu and navigate to Settings -> Apps -> Apps & Features -> Optional Features (or just press the Windows key and search for “Optional Features”)
- In the “Optional Features” window, check to make sure OpenSSH Client is in the list. Otherwise click Add a feature and add it.
- Press the Windows key or open the Start menu and search for “Services”
- In the “Services” window, double-click on “OpenSSH Authentication Agent”
- Change “Startup type” to
Automatic
- Click the Start button under “Service Status”
Step 2: Install Git
- Download Git for Windows and run the installer.
- At the “Adjusting your PATH environment” step, select the “Git from the command line and also from 3rd-party software” option.
- At the “Choosing the SSH executable” step, select “Use external OpenSSH”.
- Choose the rest of the options at your discretion.
- (Optional) Install Posh Git and other helpers.
- Right-click PowerShell and click “Run as Administrator”
- Run
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force
- Close that window and open PowerShell normally.
- Run
Install-Module posh-git -Scope CurrentUser -Force
- Run
Import-Module posh-git
- Run
Add-PoshGitToProfile -AllHosts
to load posh-git automatically whenever PowerShell starts.
Step 3: Generate the SSH Key and add it to GitHub
- Open PowerShell
- Run
ssh-keygen -t ed25519 -C "youremail@example.com"
and follow the prompts. - Run
ssh-add.exe ~\.ssh\id_ed25519
to add the key to your ssh-agent. - Run
Get-Content ~\.ssh\id_ed25519.pub | clip
. This will copy the public key to your clipboard. - Add a new SSH key in GitHub and paste the public key into the “Key” field
References
- https://phoenixnap.com/kb/generate-ssh-key-windows-10
- https://dev.to/aka_anoop/how-to-enable-openssh-agent-to-access-your-github-repositories-on-windows-powershell-1ab8