Skip to content

Instantly share code, notes, and snippets.

@foriequal0
Last active June 10, 2020 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foriequal0/55763d9177803c325904d089299f0970 to your computer and use it in GitHub Desktop.
Save foriequal0/55763d9177803c325904d089299f0970 to your computer and use it in GitHub Desktop.
git-sync
#!/usr/bin/env bash
# git-sync: pull, prune, trim all branches.
# requires: (optional) git-trim
set -euo pipefail
CURRENT=$(git branch --show-current)
do-ff() {
local INPUT_REMOTE="${1}"
local LOCAL REMOTE UPSTREAM
git branch --list --format "%(refname:short):%(upstream:remotename):%(upstream:short)" \
| while IFS=":" read -r LOCAL REMOTE UPSTREAM
do
if [ "${LOCAL}" == "${CURRENT}" ]; then
continue
fi
if [ "${REMOTE}" != "${INPUT_REMOTE}" ]; then
continue
fi
if [ -z "${UPSTREAM}" ]; then
continue
fi
if ! git show-ref --quiet "${UPSTREAM}"; then
continue
fi
if ! git merge-base --is-ancestor "${LOCAL}" "${UPSTREAM}"; then
echo "Not possible to fast-forward branch '${LOCAL}' to '${UPSTREAM}'"
continue
fi
git branch --force "${LOCAL}" "${UPSTREAM}"
done
}
git fetch --all --prune
for REMOTE in $(git remote); do
do-ff "${REMOTE}"
done
if [ -n "${CURRENT}" ]; then
UPSTREAM=$(git branch --list --format "%(upstream:short)" "${CURRENT}")
if [ -n "${UPSTREAM}" ]; then
echo "Fast-forward branch '${CURRENT}' to '${UPSTREAM}'"
git merge --ff-only "${UPSTREAM}" || true
fi
fi
if which git-trim >/dev/null; then
echo "git trim"
git trim --no-update
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment