Skip to content

Instantly share code, notes, and snippets.

@xopez
Last active June 28, 2025 02:10
Show Gist options
  • Save xopez/8099e0a8ae6a17caff3210f7ef2573d0 to your computer and use it in GitHub Desktop.
Save xopez/8099e0a8ae6a17caff3210f7ef2573d0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# === Configuration ===
GITHUB_USER="USER"
REPO_NAME="REPO"
BRANCH="main"
LAST_COMMIT_FILE=".last_commit"
# GitHub API URL for the branch
API_URL="https://api.github.com/repos/${GITHUB_USER}/${REPO_NAME}/commits/${BRANCH}"
# NFetch latest commit SHA from the remote repository
LATEST_COMMIT_SHA=$(curl -s "$API_URL" | grep '"sha":' | head -n 1 | cut -d '"' -f 4)
# Abort if no SHA was received
if [[ -z "$LATEST_COMMIT_SHA" ]]; then
echo "Error: Could not retrieve the latest commit."
exit 1
fi
# If no SHA is stored yet, store it and notify
if [[ ! -f "$LAST_COMMIT_FILE" ]]; then
echo "$LATEST_COMMIT_SHA" > "$LAST_COMMIT_FILE"
echo "First run – Commit SHA stored: $LATEST_COMMIT_SHA"
exit 0
fi
# Load previously known SHA
LAST_KNOWN_COMMIT=$(cat "$LAST_COMMIT_FILE")
# Compare
if [[ "$LATEST_COMMIT_SHA" != "$LAST_KNOWN_COMMIT" ]]; then
echo "New commit found: $LATEST_COMMIT_SHA"
echo "$LATEST_COMMIT_SHA" > "$LAST_COMMIT_FILE"
else
echo "No new commit – everything is up to date."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment