^ ^ ^ ^ =(o.o)= ~byakuren =(o.o)= m m m m
Two shell scripts (and a bonus command!) for jumping between your terminal and your repository’s web interface.
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.
Save git-url and git-open somewhere in your PATH (e.g.,
~/bin/)
Make them executable:
chmod +x ~/bin/git-url ~/bin/git-openAdd these aliases to your ~/.gitconfig:
[alias]
url = !~/bin/git-url
open = !~/bin/git-open
root = \!git rev-parse --show-toplevelAdjust the paths to match where you saved the scripts.
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/srcOpen the URL directly in your browser:
# Open current branch
$ git open
# Open a specific file
$ git open README.mdBonus alias - print the repository root:
$ pwd
/home/user/projects/myrepo/src/deeply/nested
$ git root
/home/user/projects/myrepo#!/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
fiThe script:
origingit@github.com:user/repo.git) to
HTTPS (https://github.com/user/repo).git suffixgit ls-files --full-name to
get the repo-relative path/tree/ URL for branches or
/blob/ URL for files#!/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"
fiThe script:
git-url with any arguments passed to it/dev/pts/*), opens with
xdg-open/dev/ttyv*), opens with
w3mxdg-open if detection failsShare 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.