DEV Community

Spencer Lepine
Spencer Lepine

Posted on

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:

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...