For the sake of continuous learning — and so I could use a couple work-related applications that don’t work even with WINE — I decided to spend more time in Windows. Let’s see if I can comfortably use Git from PowerShell. I’ll use the OpenSSH for Windows server for key management, since it’s already available on my system.
Setting up Git
winget knows about several Git-related packages, so my installation command needs to be specific.
PS > winget install --exact Git.Git
The installation puts Git’s
cmd folder onto
$env:Path, but PowerShell won’t see that until I refresh the variable.
PS > $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
And there it is!
PS > $env:path -split ";" C:\Windows\system32 ⋮ C:\Program Files\Git\cmd
I need to start a new session eventually, though. Can’t go around refreshing my path like that every time I open a new terminal. Though I suppose I could put this in PowerShell initialization.
I know Git Bash is a thing, but I’m trying to learn Windows — not just paste a comforting layer of UNIX duct tape over everything.
Said while writing a blog post from Vim on WSL 2.
Anyways, it looks like
ssh-keygen is accessible via PowerShell.
PS > ssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org" Generating public/private rsa key pair. Enter file in which to save the key (C:\Users\brian/.ssh/id_rsa): ⋮
I add an SSH key with the details from
C:/Users/brian/.ssh/id_rsa.pub, and check out a repo.
PS > git clone ssh://email@example.com:2222/randomgeek/random-geekery-blog.git Cloning into 'random-geekery-blog'... ⋮
That was easy enough. There are a couple bits missing from my regular Git day, though.
Conveniences with posh-git
PS > Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
PS C:\Users\brian\Projects\random-geekery-blog> Import-Module posh-git ~\Projects\random-geekery-blog [trunk ≡]>
Lovely! I can customize it later.
Getting an SSH agent with posh-sshell
I dislike entering my ssh passphrase every time I interact with a version control server.
Need to get some sort of
Looks like posh-sshell can help with that?
Install-Module posh-sshell -Scope CurrentUser
Of course, I should probably enable the
ssh-agent service from an Admin PowerShell session:
Get-Service -Name ssh-agent | Set-Service -StartupType Manual
I don’t know why “Manual.” That’s what this StackOverflow answer said, and it seems to be working.
Over in my PowerShell init, I make sure the new modules are loaded, set some handy aliases for using my SSH keys, and start the SSH Agent.
Import-Module posh-git Import-Module posh-sshell Set-Alias ssh-agent "$env:WinDir\System32\OpenSSH\ssh-agent.exe" Set-Alias ssh-add "$env:WinDir\System32\OpenSSH\ssh-add.exe" Start-SshAgent -Quiet