Skip to content

Instantly share code, notes, and snippets.

@bencord0
Last active June 7, 2022 01:32
Show Gist options
  • Save bencord0/034be3ffe662f74db103f6ee53e6aac0 to your computer and use it in GitHub Desktop.
Save bencord0/034be3ffe662f74db103f6ee53e6aac0 to your computer and use it in GitHub Desktop.
Reset branch
#!/bin/bash
# Resets the `staging` branch to the contents of the `production` branch,
# all while maintaining history of both trees, harshly.
PRODUCTION="${1}"
STAGING="${2}"
if [ -z "${PRODUCTION}" -o -z "${STAGING}" ]; then
echo "Usage: reset.sh <production> <staging>"
exit 1
fi
if git diff --exit-code "${PRODUCTION}"..."${STAGING}" > /dev/null; then
echo "No diff between branches"
exit 0
fi
# Create a new lineage, from the tree that we want to keep
git switch -c "new-${STAGING}" "${PRODUCTION}"
# Merge, keeping history of both branches, but discarding any diffs in STAGING
git merge -s ours "${STAGING}" -m "Resetting ${STAGING} to ${PRODUCTION}"
# STAGING is obsolete, can be easily removed
git branch -d "${STAGING}"
# so that "new-STAGING" becomes "STAGING"
git branch -m "${STAGING}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment