git-url & git-open

Two shell scripts (and a bonus command!) for jumping between your terminal and your repository’s web interface.

The Problem

You’re working in a git repository. You want to: - Share a link to a file with a coworker

You could navigate to GitHub, find the repo, click through to the right branch and file… or you could just type git open.

Installation

  1. Save git-url and git-open somewhere in your PATH (e.g., ~/bin/)

  2. Make them executable:

    chmod +x ~/bin/git-url ~/bin/git-open
  3. Add these aliases to your ~/.gitconfig:

    [alias]
        url = !~/bin/git-url
        open = !~/bin/git-open
        root = \!git rev-parse --show-toplevel

    Adjust the paths to match where you saved the scripts.

Usage

git url

Generate a web URL for the current repository:

# URL for the current branch
$ git url
https://github.com/user/repo/tree/main

# URL for a specific file
$ git url src/main.rs
https://github.com/user/repo/blob/main/src/main.rs

# URL for a directory
$ git url src/
https://github.com/user/repo/blob/main/src

git open

Open the URL directly in your browser:

# Open current branch
$ git open

# Open a specific file
$ git open README.md

git root

Bonus alias - print the repository root:

$ pwd
/home/user/projects/myrepo/src/deeply/nested

$ git root
/home/user/projects/myrepo

How It Works

git-url

#!/bin/bash
REMOTE_URL=$(git remote get-url origin 2>/dev/null || echo "No remote origin found")
BRANCH=$(git branch --show-current 2>/dev/null || echo "HEAD")

if [ "$REMOTE_URL" = "No remote origin found" ]; then
    echo "$REMOTE_URL"
    exit 1
fi

# Convert SSH to HTTPS if needed
if echo "$REMOTE_URL" | grep -q "^git@"; then
    REMOTE_URL=$(echo "$REMOTE_URL" | sed 's/^git@\([^:]*\):/https:\/\/\1\//' | sed 's/\.git$//')
else
    REMOTE_URL=$(echo "$REMOTE_URL" | sed 's/\.git$//')
fi

if [ -z "$1" ]; then
    echo "$REMOTE_URL/tree/$BRANCH"
else
    # Handle file path
    if [ -f "$1" ] || [ -d "$1" ]; then
        REL_PATH=$(git ls-files --full-name "$1" 2>/dev/null || echo "$1")
        echo "$REMOTE_URL/blob/$BRANCH/$REL_PATH"
    else
        echo "File not found: $1"
        exit 1
    fi
fi

The script:

  1. Gets the remote URL from origin
  2. Gets the current branch name
  3. Converts SSH URLs (git@github.com:user/repo.git) to HTTPS (https://github.com/user/repo)
  4. Strips the .git suffix
  5. If given a file path, uses git ls-files --full-name to get the repo-relative path
  6. Outputs a /tree/ URL for branches or /blob/ URL for files

git-open

#!/bin/sh

# Get the URL using git-url
URL=$(~/bin/sh/git-url "$@")
if [ $? -ne 0 ]; then
    echo "$URL"
    exit 1
fi

# Check if we're in X11/Xorg or TTY environment
client_tty=$(tmux display-message -p "#{client_tty}" 2>/dev/null)

if echo "$client_tty" | grep -q "^/dev/pts/"; then
    # We're in X11/Xorg - use xdg-open
    xdg-open "$URL"
elif echo "$client_tty" | grep -q "^/dev/ttyv"; then
    # We're in TTY - use w3m
    w3m "$URL"
elif [ -t 0 ]; then
    # Fallback TTY check - use w3m
    w3m "$URL"
else
    # Default to xdg-open
    xdg-open "$URL"
fi

The script:

  1. Calls git-url with any arguments passed to it
  2. Detects the environment by checking the tmux client TTY
  3. In graphical environments (/dev/pts/*), opens with xdg-open
  4. In console/TTY environments (/dev/ttyv*), opens with w3m
  5. Falls back to xdg-open if detection fails

Why This Is Useful

Share code instantly: Need to point someone at a file? git url src/api.ts | xclip and paste.

Context switching: Deep in the terminal but need to review a PR? git open gets you there.

Works with SSH remotes: Most git hosting uses SSH for push access, but web URLs need HTTPS. The script handles the conversion automatically.

Environment-aware: Works whether you’re in a graphical desktop or a raw TTY console.

No dependencies: Pure shell. No package managers, no runtimes, no config files.

Composable: git url just prints a URL - pipe it to xclip, curl, wget, whatever you need.