Skip to content

Instantly share code, notes, and snippets.

@yifeiyin
Created October 30, 2025 22:16
Show Gist options
  • Save yifeiyin/036e70c93c2b45ecbf1129316746ea48 to your computer and use it in GitHub Desktop.
Save yifeiyin/036e70c93c2b45ecbf1129316746ea48 to your computer and use it in GitHub Desktop.
Publish commit to github gist
# --- Publish a git commit as a gist patch ---
gist_commit() {
local sha="$1"
local desc="${2:-"Patch for commit $sha"}"
if [ -z "$sha" ]; then
echo "Usage: gist_commit <commit-sha> [description]"
return 1
fi
echo "Creating gist for commit $sha..."
gh gist create <(git show "$sha") -d "$desc" -p
}
# --- Apply a gist patch to current repo ---
gist_apply() {
local gist_url="$1"
if [ -z "$gist_url" ]; then
echo "Usage: gist_apply <gist-url>"
return 1
fi
# Normalize the URL in case it's missing .git or raw format
if [[ "$gist_url" =~ gist\.github\.com/.+/.+ ]]; then
# Try to find a raw file URL automatically
local raw_url
raw_url=$(curl -s "$gist_url" | grep -oE 'https://gist.githubusercontent.com/[^"]+' | head -n1)
if [ -z "$raw_url" ]; then
echo "❌ Could not find a raw patch URL in that gist."
return 1
fi
echo "Applying patch from: $raw_url"
curl -s "$raw_url" | git apply -
else
echo "❌ Invalid gist URL. Expected format like:"
echo " https://gist.github.com/username/abcdef123456"
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment