Quickly Open GitHub Repo in Browser From Terminal

Quickly Open GitHub Repo in Browser From Terminal

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

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)

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

$ vim repo-open

Now paste the script contents into the file editor:

#!/bin/bash

git remote -v | awk ‘/origin.*push/ {print $2}’ | xargs open

Save the file:

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

Create the executable:

$ chmod +x repo-open

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

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
}

Script for Windows:

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

# GIT: open remote repository using Firefox
function fbrowse {
NC=’33[0m’ # No Color
SB=’33[1;34m’ # Sky Blue
PP=’33[1;35m’ # Purple
fbrowsevar=$(git config –get remote.origin.url)
printf n${PP}${SB}fbrowse:${NC} Firefoxn
printf ${fbrowsevar}n
start firefox $fbrowsevar
}

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:

LinkedIn: /in/spencerlepine

Email: spencer.sayhello@gmail.com

Portfolio: spencerlepine.com

GitHub: @spencerlepine

Twitter: @spencerlepine

Leave a Reply

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