Skip to content

Instantly share code, notes, and snippets.

@jphalip
Created December 20, 2022 22:37
Show Gist options
  • Save jphalip/b7d0bc22c908eac956d2adae733796c8 to your computer and use it in GitHub Desktop.
Save jphalip/b7d0bc22c908eac956d2adae733796c8 to your computer and use it in GitHub Desktop.
# Takes two parameters: target branch and commit message.
# Does a hard reset on the target branch then adds a single commit with the diff
# between the two branches. Sort of simulates a rebase but without having to
# resolve potential conflicts in intermediary commits.
function git-hard-rebase() {
target_branch=$1
if git rev-parse --quiet --verify ${target_branch} > /dev/null; then
else
echo "Branch name ${target_branch} does not exist."
return 1
fi
message=$2
if test -z "$message"
then
echo "Message is empty"
return 1
fi
current_branch=$(git branch --show-current)
file_diff=$(mktemp "/tmp/${current_branch}.${target_branch}.XXXXXX")
echo ${file_diff}
git diff ${target_branch} > ${file_diff}
git branch "backup/${current_branch}-$(date '+%Y_%m_%d-%H_%M_%S')"
git reset --hard ${target_branch}
git apply ${file_diff}
git add -A
git commit -m ${message}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment