This script automates the process of synchronizing a Git repository by ensuring all remote branches are tracked locally, pulling updates with fast-forward-only behavior, updating submodules (when present), and optionally deleting local branches that no longer exist on the remote.
- Clones the repo (with submodules) if not already present
- Fetches and prunes all remote branches
- Tracks all
origin/*
branches as local branches if missing - Checks out each branch:
- Performs a fast-forward-only
git pull
- Updates submodules (if any) safely
- Performs a fast-forward-only
- Prompts the user (one by one) to delete any local branches not found on
origin
- Stashes local changes before switching branches and restores them at the end
- Can be safely piped from a remote source (e.g.
curl | bash
) - Designed to avoid destructive operations (no
git clean
ordeinit
)
./sync_all_branches.sh
./sync_all_branches.sh https://github.com/your/repo.git
./sync_all_branches.sh https://github.com/your/repo.git my-clone-dir
(This is a long command, make sure you get the "| bash
" at the end)
curl -fsSL https://gist.githubusercontent.com/lbussy/e14fba1572de53df46701b9d772f244f/raw/sync_all.sh | bash
(This is a long command, make sure you get the whole line)
You can use just the repo url at the end, or optionally a target directory
curl -fsSL https://gist.githubusercontent.com/lbussy/e14fba1572de53df46701b9d772f244f/raw/sync_all.sh | bash -s -- https://github.com/your/repo.git my-dir
- The script avoids destructive cleanup (no
git clean -fdx
). - Prompts use
/dev/tty
to ensure interactive input even when piped. - All output messages end with a period (no ellipses or exclamation points).
- Designed to work on Debian-based systems with POSIX-compliant
bash
.
MIT License. Use at your own risk.