Thursday, November 7, 2024
Featured Technology  

Open GIT URL from Windows Terminal

This post assumes a few things:

  • You are running Windows
  • You have PowerShell (preferably with Windows Terminal installed)
  • You have Code installed and on your executable path
  • You have git installed and on your executable path

OK, are you still with me?

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:

gist

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:

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 Start-Process chrome $repoUrl to Start-Process msedge $repoUrl you will open Microsoft Edge instead of Google Chrome.

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.

Similar Posts

One thought on “Open GIT URL from Windows Terminal

Comments are closed.