Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Created June 4, 2025 22:14
Show Gist options
  • Save ericboehs/6cf26217da7270692ef498cf1962997f to your computer and use it in GitHub Desktop.
Save ericboehs/6cf26217da7270692ef498cf1962997f to your computer and use it in GitHub Desktop.
Watch and pull script that monitors git repository for changes and opens localhost:3000 when updates are detected
#!/bin/bash
# Function to open localhost:3000
open_localhost() {
echo "Changes detected! Opening localhost:3000..."
if command -v open >/dev/null 2>&1; then
open http://localhost:3000
elif command -v xdg-open >/dev/null 2>&1; then
xdg-open http://localhost:3000
else
echo "Cannot open browser automatically. Please visit http://localhost:3000"
fi
}
# Get initial commit hash
last_commit=$(git rev-parse HEAD)
echo "Starting git pull --rebase watcher (every 15 seconds)..."
echo "Initial commit: $last_commit"
while true; do
echo "Checking for updates..."
# Perform git pull --rebase
git pull --rebase
# Get current commit hash
current_commit=$(git rev-parse HEAD)
# Check if commit changed
if [ "$current_commit" != "$last_commit" ]; then
echo "Repository updated from $last_commit to $current_commit"
open_localhost
last_commit=$current_commit
else
echo "No changes detected"
fi
# Wait 15 seconds
sleep 15
done
@ericboehs
Copy link
Author

Watch and Pull Script

A bash script that continuously monitors a git repository for changes and automatically opens localhost:3000 when updates are detected.

Features

  • Polls git repository every 15 seconds for new commits
  • Performs to fetch latest changes
  • Cross-platform browser opening (macOS, Linux)
  • Graceful fallback when browser can't be opened automatically

Usage

Make the script executable and run:

chmod +x watch_and_pull.sh
./watch_and_pull.sh

How it works

  1. Records the initial git commit hash
  2. Every 15 seconds:
    • Runs git pull --rebase
    • Checks if the commit hash changed
    • Opens http://localhost:3000 if changes detected
    • Waits 15 seconds before next check

Perfect for development workflows where you want to automatically view changes when collaborators push updates.

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