Skip to content

Instantly share code, notes, and snippets.

@jaysoffian
Created May 6, 2012 13:06
Show Gist options
  • Save jaysoffian/2622264 to your computer and use it in GitHub Desktop.
Save jaysoffian/2622264 to your computer and use it in GitHub Desktop.
A script for rebasing a merge
#!/bin/sh -x
# remerge <onto> [<merge_commit>]
# e.g.: remerge origin/trunk
# merge_commit defaults to HEAD
onto=$1
mc=${2:-HEAD}
mc_sha=$(git rev-parse $mc) # original merge commit
p1_sha=$(git rev-parse $onto) # what we want its new first parent to be
p2_sha=$(git rev-parse $mc^2) # keep the original second parent
# write out a script to finish the merge in case the merge step has conflicts
cat >finish_merge.sh<<__EOF__
tree=\$(git log -1 HEAD --pretty=%T)
git reset --hard \$(git cat-file commit $mc_sha | sed '1,/^$/d' | git commit-tree \$tree -p $p1_sha -p $p2_sha)
__EOF__
git checkout $mc_sha # checkout the merge commit
git merge $p1_sha || { echo "Resolve and commit the merge; then run finish_merge.sh"; exit 1; }
# recreate the original merge using this new tree and new first parent
. ./finish_merge.sh
rm -f finish_merge.sh
@jaysoffian
Copy link
Author

jaysoffian commented Sep 17, 2019

Looks like -m was added in late 2007. Not sure why I wrote the script the way I did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment