Skip to content

Instantly share code, notes, and snippets.

@khoa-le
Created January 21, 2026 07:17
Show Gist options
  • Select an option

  • Save khoa-le/7a47ad19810af8638b4ab1bff4e5b328 to your computer and use it in GitHub Desktop.

Select an option

Save khoa-le/7a47ad19810af8638b4ab1bff4e5b328 to your computer and use it in GitHub Desktop.
#!/bin/bash
# gist-sync - Push file changes to GitHub Gist immediately
# Usage: gist-sync <file> [gist-id]
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored messages
info() { echo -e "${BLUE}ℹ${NC} $1"; }
success() { echo -e "${GREEN}✅${NC} $1"; }
error() { echo -e "${RED}❌${NC} $1"; }
warn() { echo -e "${YELLOW}⚠${NC} $1"; }
# Check if gh CLI is installed
if ! command -v gh &> /dev/null; then
error "GitHub CLI (gh) is not installed"
echo ""
echo "Install it with:"
echo " macOS: brew install gh"
echo " Linux: https://github.com/cli/cli#installation"
echo ""
echo "After installation, run: gh auth login"
exit 1
fi
# Check if authenticated
if ! gh auth status &> /dev/null; then
error "Not authenticated with GitHub"
echo ""
echo "Run: gh auth login"
exit 1
fi
# Parse arguments
FILE_PATH="$1"
GIST_ID="$2"
if [ -z "$FILE_PATH" ]; then
error "No file specified"
echo ""
echo "Usage: gist-sync <file> [gist-id]"
echo ""
echo "Examples:"
echo " gist-sync myfile.txt # Create new gist"
echo " gist-sync myfile.txt abc123def456 # Update existing gist"
echo " gist-sync myfile.txt --watch # Watch and auto-sync"
exit 1
fi
# Check if file exists
if [ ! -f "$FILE_PATH" ]; then
error "File not found: $FILE_PATH"
exit 1
fi
# Get absolute path and filename
FILE_ABS=$(cd "$(dirname "$FILE_PATH")" && pwd)/$(basename "$FILE_PATH")
FILE_NAME=$(basename "$FILE_PATH")
# Function to sync file to gist
sync_to_gist() {
local file="$1"
local gist_id="$2"
if [ -z "$gist_id" ]; then
# Create new gist
info "Creating new gist for $FILE_NAME..."
RESULT=$(gh gist create "$file" --public 2>&1)
if [ $? -eq 0 ]; then
GIST_URL=$(echo "$RESULT" | grep -o 'https://gist.github.com/[a-zA-Z0-9/]*')
GIST_ID=$(basename "$GIST_URL")
success "Gist created: $GIST_URL"
# Save gist ID for future use
echo "$GIST_ID" > "${file}.gist-id"
info "Gist ID saved to ${FILE_NAME}.gist-id"
echo "$GIST_ID"
else
error "Failed to create gist"
echo "$RESULT"
return 1
fi
else
# Update existing gist
info "Updating gist $gist_id..."
if gh gist edit "$gist_id" "$file" 2>&1; then
success "Gist updated: https://gist.github.com/$gist_id"
else
error "Failed to update gist"
return 1
fi
fi
}
# Handle watch mode
if [ "$GIST_ID" = "--watch" ]; then
# Check for saved gist ID
if [ -f "${FILE_ABS}.gist-id" ]; then
GIST_ID=$(cat "${FILE_ABS}.gist-id")
info "Using saved gist ID: $GIST_ID"
else
# Create new gist first
GIST_ID=$(sync_to_gist "$FILE_ABS" "")
fi
# Check if fswatch is installed
if ! command -v fswatch &> /dev/null; then
error "fswatch is not installed (required for --watch mode)"
echo ""
echo "Install it with: brew install fswatch"
exit 1
fi
info "👀 Watching $FILE_NAME for changes... (Ctrl+C to stop)"
echo ""
# Watch for changes
fswatch -o "$FILE_ABS" | while read -r change; do
echo "$(date '+%Y-%m-%d %H:%M:%S') - Change detected"
sync_to_gist "$FILE_ABS" "$GIST_ID"
echo ""
done
else
# Check for saved gist ID if not provided
if [ -z "$GIST_ID" ] && [ -f "${FILE_ABS}.gist-id" ]; then
GIST_ID=$(cat "${FILE_ABS}.gist-id")
info "Using saved gist ID: $GIST_ID"
fi
# Single sync
sync_to_gist "$FILE_ABS" "$GIST_ID"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment