Created
May 11, 2022 16:56
-
-
Save Broxzier/645648135bb8e4dd7383115bce97c519 to your computer and use it in GitHub Desktop.
A shell script to easily swap the parents of a merge commit, so that you can avoid having files that you changed be touched and force a rebuild. Place this in your `~/bin` folder and you can simply use `git swap-merge`
This file contains hidden or 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 | |
# Check if HEAD is a merge commit | |
numParents=$(git show -s --format=%p HEAD | wc -w) | |
if [[ "$numParents" != "2" ]]; then | |
echo "HEAD is not a merge commit" | |
exit 1 | |
fi | |
# Format the new commit message | |
lastCommitMsg=$(git log --pretty=%B -1) | |
newCommitMsg=$(echo $lastCommitMsg | sed -re 's/^Merge .*?branch (.+) into (.+)$/Merge branch \2 into \1/g') | |
if [[ "$lastCommitMsg" == "$newCommitMsg" ]]; then | |
echo "HEAD does not have a standard merge commit message" | |
exit 2 | |
fi | |
# Create a new commit with the parents swapped, and reset HEAD to this | |
git reset $(git commit-tree -p HEAD^2 -p HEAD^1 -m "$newCommitMsg" "HEAD^{tree}") | |
# Success | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With this, you can simply merge the main branch into your feature branch, then swap the parents, to avoid having your files touched again.
Before swap -> After swap