Safe Git Sync Scripts for Multi-Computer Workflows
When you switch machines often, “just pull and push” eventually causes branch confusion or accidental overwrite. A lightweight sync script with safety checks solves most of this.
Step 1: Refuse to run with a dirty working tree
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "Working tree is dirty; aborting sync" >&2
exit 1
fi
Step 2: Pull with rebase and explicit remote branch
git fetch origin
git rebase origin/main
Step 3: Push and log sync status
git push origin HEAD:main
printf "%s synced %s\n" "$(hostname)" "$(date -u +%FT%TZ)" >> .sync-log
Pitfalls
- Blind
git pullon whichever branch is currently checked out. - Running sync scripts that auto-commit unknown changes.
- No traceability of which machine synced last.
Validation
- Script exits safely when local changes exist.
- Rebase conflicts are surfaced and never auto-resolved silently.
- Sync log provides machine/time audit trail.