Skip to content

Instantly share code, notes, and snippets.

@mipearson
Created October 10, 2011 06:42
Show Gist options
  • Save mipearson/1274761 to your computer and use it in GitHub Desktop.
Save mipearson/1274761 to your computer and use it in GitHub Desktop.
Oops! I committed this to the wrong branch in git!
# Oops! I committed this to the wrong branch.
# Here's how to fix without needing to do a messy git revert.
# Assumptions:
# * you have no uncommitted changes
# * you haven't pushed or pulled yet
# * you haven't made any further commits
DEST = bar # branch you intended to commit to
SRC = foo # branch you actually committed to
git checkout $DEST # switch to the destination branch
git cherry-pick $SRC # copy the commit to the destination branch
git checkout $SRC # switch back to the source branch
git reset --hard HEAD^ # roll back the source branch one commit
# alternatively, you can use a git-merge instead of a
# git-cherry-pick if you know what you're doing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment