Last active
June 7, 2022 01:32
-
-
Save bencord0/034be3ffe662f74db103f6ee53e6aac0 to your computer and use it in GitHub Desktop.
Reset branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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