Last active
February 12, 2026 18:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Prints a Markdown link to 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-link [-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}" | |
| LINK_SUFFIX="" | |
| 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}" | |
| LINK_SUFFIX=" (lines ${START}-${END})" | |
| else | |
| FILE_URL="${FILE_URL}#L${LINES}" | |
| LINK_SUFFIX=" (line ${LINES})" | |
| fi | |
| fi | |
| FILE_LINK="[\`${FILE_PATH}\`${LINK_SUFFIX}](${FILE_URL})" | |
| printf '%s' "$FILE_LINK" | pbcopy | |
| echo "Copied: $FILE_LINK" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Get GitHub file URL at latest commit and copy it to clipboard:
Get GitHub file Markdown link at latest commit and copy it to clipboard:
With a specific line number highlighted:
With a specific line number range highlighted:
Setup
Recommended usage so you have these globally available:
.bashsuffix.chmod +x <file>).bindirectory that's in your$PATH.