Categories
geek programming software tips tools

open a Git repositories’ website from the command line

Are you using Git a lot from the command line? Isn’t it annoying that you have to open a browser and click your way to the GitHub, GitLab or Azure DevOps repo website to create a pull request or do something else that can’t be done in your shell?
To solve that problem, I have a PowerShell function that opens the Git repository straight from your current folder in your shell, in your default browser. It checks the Git config for the origin URL, and opens it automatically. It’s super handy to quickly check the online repo, create pull requests etc.

Copy and paste the code below in a .ps1 script, and you’re set.

function Open-RepoInBrowser
{
    $url = git config --get remote.origin.url

    if ($url -like "*git@*")
    {
        # Get the URL from an SSH cloned repo.
        $url = $url -replace 'git@', ''
        $url = "https://" + ($url -replace ':', '/')
    }

    if ($url -eq $null) 
    {
        Write-Warning "No URL found. Make sure you are in the root of the Git repository."
        return
    }

    Write-Host "Opening URL $url"
    start $url
}

# Execute the function
Open-RepoInBrowser

Now navigate into a Git repository folder, run your script, and see that website open up in whatever your default browser is.
Note that PowerShell also runs on most Linux versions these days, so nothing is stopping you from using this easy shortcut.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.