Open Windows Terminal (Which ever PowerShell profile you use git from) and type:
> code $PROFILE
Code is the Microsoft Visual Studio Code editor, you could use notepad if you wanted, but I like Code.
$PROFILE is a variable in PowerShell that points to your Profile.ps1 file.
Find a free space in your profile.ps1 file and add this function:
function GitW {
$repoUrl = git config --get remote.origin.url
if(!$repoUrl) {
Write-Output " No git URL found"
break
}
# Start-Process chrome $repoUrl
# Update (See Below) - by removing the browser,
# Windows will use the default browser on your computer. Win!
Start-Process $repoUrl
}
At this point, you can refresh your PowerShell Profile
> . $PROFILE
Now, you can type gitw
(for git webpage) from any folder, and if you are in a git folder, the origin remote URL will open in Chrome.
and if you are in a non-git folder, you’ll get a note telling you – so handy!
and since you’ve been drooling over my awesome terminal prompt, go check out these articles from Scott Hanselman so you can level up your own:
- Take your Windows Terminal and PowerShell to the next level with Terminal Icons
- My Ultimate PowerShell prompt with Oh My Posh and the Windows Terminal
- How to make pretty prompt in Windows Terminal with Powerline, Nerd Fonts, Cascadia Code, WSL, and oh-my-posh
I love quick shortcuts like this that make your day to day activites faster, easier and more enjoyable. What about you? What are some of your favorite time savers?
I do think I could get this whole PowerShell script down to one line, then I could probably alias it as a git command, something like this:
// Doesn't work
git config --global alias.www = "$repoURL = git config --get remote.origin.url; if(!$repoURL) { Write-Output 'No git URL ' } else { Start-Process chrome $repoURL }"
Maybe someone with better git-fu than me can work this out, at least for now, the PowerShell function is working for me.
Enjoy!
Update (1.16.22): Thanks to an active twitter thread a few things were pointed out.
If you change
to Start-Process chrome $repoUrl
you will open Microsoft Edge instead of Google Chrome. Start-Process msedge $repoUrl
Thanks to Tim for pointing out that if you omit the browser declaration completely, then windows will use the default browser on your system. I’ve updated the gist to reflect this. Start-Process $repoUrl # Opens http handler (browser) on Windows
They way this function was written, will only work if you use git with https remotes, not SSH. I’ve not used SSH much with my git interactions; if you want to give SSH a shot Greg has a great start, and Labelle has a much more comprehensive PowerShell solution to account for SSH.
Ben pointed out this is a PowerShell function – not just Windows Terminal, and so will work with any terminal, not just Windows Terminal 👍
Finally, thanks to Jan De Dobbeleer for running with this idea and creating a PR for oh-my-posh. I’d love to see this in oh-my-posh out of the box! Awesome. This is quite possibly the thing I love the most about open web (and OSS in general). Throwing an idea out there, and seeing it work its way it to a tool that you use every day.
One thought on “Open GIT URL from Windows Terminal”
Comments are closed.