Skip to content

Instantly share code, notes, and snippets.

@therealbnut
Created August 27, 2015 08:48
Show Gist options
  • Save therealbnut/2cbef756cae217297b0f to your computer and use it in GitHub Desktop.
Save therealbnut/2cbef756cae217297b0f to your computer and use it in GitHub Desktop.
Swaps the last two commits, perhaps, who knows? Have fun!
[alias]
swap-last-two-commits = "!f() {\n\
set -eu \n\
if [ ! -z \"`git diff HEAD`\" ]; then \n\
echo \"Found uncommited changes... exiting.\" 1>&2 \n\
exit 1 \n\
fi \n\
HEAD_0=\"$(git rev-parse --verify HEAD)\" \n\
HEAD_1=\"$(git rev-parse --verify HEAD~1)\" \n\
echo \"swapping ${HEAD_0} and ${HEAD_1}\" \n\
git reset --soft HEAD~2 \n\
git commit -C \"${HEAD_1}\" 1> /dev/null \n\
HEAD_0_1=\"$(git rev-parse --verify HEAD)\" \n\
git reset --hard HEAD~1 1> /dev/null \n\
git cherry-pick \"${HEAD_0}\" 1> /dev/null \n\
git cherry-pick --strategy=recursive -X theirs \"${HEAD_0_1}\" 1> /dev/null \n\
if [ ! -z \"`git diff \"${HEAD_0}\"`\" ]; then \n\
echo \"HEAD differs from \"${HEAD_0}\" run 'git reset --hard \"${HEAD_0}\"' to undo. \" 1>&2 \n\
exit 1 \n\
fi \n\
}; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment