Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Last active February 12, 2026 18:39
Show Gist options
  • Select an option

  • Save felixarntz/d4bb83eed27a1e411b47bad6d47f3e04 to your computer and use it in GitHub Desktop.

Select an option

Save felixarntz/d4bb83eed27a1e411b47bad6d47f3e04 to your computer and use it in GitHub Desktop.
Use these scripts to get stable URLs to a given file in your repo, optionally highlighting certain lines. These kinds of links are useful for discussions on GitHub, or pointing to specific references.
#!/bin/bash
#
# Prints the GitHub URL for a given file at the latest commit hash and copies it to the clipboard.
LINES=""
FILE_PATH=""
while [ $# -gt 0 ]; do
case $1 in
-l) LINES="$2"; shift 2 ;;
*) FILE_PATH="$1"; shift ;;
esac
done
if [ -z "$FILE_PATH" ]; then
echo "Usage: gh-file-url [-l <line|start-end>] <path-relative-to-repo-root>"
exit 1
fi
REMOTE_URL=$(git remote get-url origin)
REPO=$(echo "$REMOTE_URL" | sed -E 's#(git@github\.com:|https://github\.com/)##; s/\.git$//')
COMMIT=$(git rev-parse HEAD)
FILE_URL="https://github.com/${REPO}/blob/${COMMIT}/${FILE_PATH}"
if [ -n "$LINES" ]; then
if echo "$LINES" | grep -q '-'; then
START=$(echo "$LINES" | cut -d'-' -f1)
END=$(echo "$LINES" | cut -d'-' -f2)
FILE_URL="${FILE_URL}#L${START}-L${END}"
else
FILE_URL="${FILE_URL}#L${LINES}"
fi
fi
printf '%s' "$FILE_URL" | pbcopy
echo "Copied: $FILE_URL"
@felixarntz
Copy link
Author

felixarntz commented Feb 12, 2026

Usage

Get GitHub file URL at latest commit and copy it to clipboard:

gh-file-url packages/ai/src/generate-text/generate-text.ts

Get GitHub file Markdown link at latest commit and copy it to clipboard:

gh-file-link packages/ai/src/generate-text/generate-text.ts

With a specific line number highlighted:

gh-file-url packages/ai/src/generate-text/generate-text.ts -l 12
gh-file-link packages/ai/src/generate-text/generate-text.ts -l 12

With a specific line number range highlighted:

gh-file-url packages/ai/src/generate-text/generate-text.ts -l 10-15
gh-file-link packages/ai/src/generate-text/generate-text.ts -l 10-15

Setup

Recommended usage so you have these globally available:

  1. Download these files.
  2. Rename them to drop the .bash suffix.
  3. Make them executable (chmod +x <file>).
  4. Put them in a bin directory that's in your $PATH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment