DEV Community

Spencer Lepine
Spencer Lepine

Posted on

1

Quickly Open GitHub Repo in Browser From Terminal

Blog Post Thumbnail

I work a lot with the Git CLI and GitHub repository cloned on my local machine. I need a fast way to open the repository web page in the browser. Here is how I solved this, specifically on macOS.

To start, the the quickest way to get the remote url is the following bash command:

git remote -v | awk '/origin.*push/ {print $2}' | xargs open
Enter fullscreen mode Exit fullscreen mode

That command alone is not very helpful, since it will be difficult to memorize and type out repeatedly.

Instead, we can create a user-friendly command to use in the macOS terminal. By creating a custom named script in the bin directory, the terminal will execute it when the command is used.

First, navigate to the bin directory:

$ cd ~/../../usr/local/bin

# Make sure this path matches up with your 
# configuration for the terminal (e.g. PATH=$PATH:$HOME/bin)
Enter fullscreen mode Exit fullscreen mode

Now create the script file, here I named the command repo-open:

$ vim repo-open
Enter fullscreen mode Exit fullscreen mode

Now paste the script contents into the file editor:

 #!/bin/bash

git remote -v | awk '/origin.*push/ {print $2}' | xargs open
Enter fullscreen mode Exit fullscreen mode

Save the file:

  • press ‘ESC’
  • press ‘SHIFT’ + ‘:’
  • type ‘wq’ + ENTER

Create the executable:

$ chmod +x repo-open
Enter fullscreen mode Exit fullscreen mode

That’s it! Now you can run the new script in the terminal. If we are in a directory with a .git folder, we can run repo-open, and it will open the remote URL in the default browser.

$ repo-open
# opens new page in the browser
Enter fullscreen mode Exit fullscreen mode

Optionally, you can dig a little deeper into writing these scripts. Here are a few examples for Mac and Windows:

Bash script for Mac:

function gbrowse {
    gbrowsevar=$(git config --get remote.origin.url)
    printf "${gbrowsevar}"
    start $gbrowsevar
}
Enter fullscreen mode Exit fullscreen mode

Script for Windows:

# GIT: open remote repository using Google Chrome
function gbrowse {
    NC='\033[0m' # No Color
    SB='\033[1;34m' # Sky Blue
    PP='\033[1;35m' # Purple
    gbrowsevar=$(git config --get remote.origin.url)
    printf "\n${PP}${SB}gbrowse:${NC} Chrome\n"
    printf "${gbrowsevar}\n"
    start chrome $gbrowsevar
}

# GIT: open remote repository using Firefox
function fbrowse {
    NC='\033[0m' # No Color
    SB='\033[1;34m' # Sky Blue
    PP='\033[1;35m' # Purple
    fbrowsevar=$(git config --get remote.origin.url)
    printf "\n${PP}${SB}fbrowse:${NC} Firefox\n"
    printf "${fbrowsevar}\n"
    start firefox $fbrowsevar
}
Enter fullscreen mode Exit fullscreen mode

That’s all for today, I hope this article was helpful. If you have any questions, feel free to connect with me.

Follow my journey or connect with me here:

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (1)

Collapse
 
ccoveille profile image
Christophe Colombier

I would suggest you to use GitHub CLI

cli.github.com/manual/gh_browse

Or Gitlab CLI

gitlab.com/gitlab-org/cli/-/blob/m...

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay