Last active
September 6, 2018 08:55
-
-
Save eddiemoya/5456992 to your computer and use it in GitHub Desktop.
Flip the last two commits in a branch using git-rebase, git-cherry-pick, git-update-ref, git-name-rev, and git-rev-parse. Interesting exercise using quite a bit of plumbing commands.
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 | |
branch=$(git name-rev --name-only HEAD) | |
git rebase --onto HEAD~2 HEAD~1 HEAD | |
git cherry-pick ORIG_HEAD~1 | |
git update-ref refs/heads/$branch $(git rev-parse HEAD) | |
git checkout --quiet $branch | |
# Instead of creating an independant bash script with the code above, | |
# consider simply creating a git alias using the command below. | |
# | |
# USAGE: git flip-last | |
git config --global alias.flip-last '!branch=$(git name-rev --name-only HEAD); git rebase --quiet --onto HEAD~2 HEAD~1 HEAD; git cherry-pick ORIG_HEAD~1; git update-ref refs/heads/$branch $(git rev-parse HEAD); git checkout --quiet $branch' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, I have a concern about your gist : if something fails during either rebase or cherry-pick, you don't help the user and, worst, you reset his actual branch to the currently-in-failure sha1
I fixed those concerns in my fork here