Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Pulled the wrong branch, then pushed the merge. How to undo?

I recently made a mistake with my git merge strategy, and didn't notice the result error(s) until I had already continued to work on the project and made more commits. For the sake of brevity, let's say I have two branches, hotfix and new_feature. I did something like the following:

git checkout new_feature
git commit -m "Commit to the feature branch"
git checkout hotfix
git pull origin new_feature # Uh oh, I pulled the wrong branch!
git commit -m "Commit to the hotfix"
git push origin hotfix # Uh Oh, pushed the bad merge!

After the events above, other developers also made commits to the Hotfix branch. Now, we have a hotfix we want to push out, but can't because it contains feature work that isn't complete.

I've seen a lot of similar questions about this situation, but none that match 100% (most of them involve undoing the commit directly after the bad pull, without pushing first. I've gone one step further, and really borked things.

Any step-by-step guides on how I can remedy this?

Thanks!

Edit: many other answers mention git reflog, so here is a modified copy of mine. I've changed the hashes and branch names to match my example above.

aaaaaaa HEAD@{0}: checkout: moving from new_feature to hotfix
bbbbbbb HEAD@{1}: checkout: moving from hotfix to new_feature
aaaaaaa HEAD@{2}: commit: Added analytics tracking
ccccccc HEAD@{3}: commit (merge): Fixed a merge issue in compiled css # BAD MERGE?
ddddddd HEAD@{4}: checkout: moving from new_feature to hotfix
eeeeeee HEAD@{5}: commit: Finished updating the events for the header
ddddddd HEAD@{6}: checkout: moving from hotfix to new_feature
ddddddd HEAD@{7}: checkout: moving from new_feature to hotfix

In the reflog above, HEAD@{3} looks to be the bad merge. I was on the hotfix branch, and pulled in the new_feature branch, which gave merge conflicts. I fixed them, and pushed. What would I do to fix this?

like image 396
Mike Trpcic Avatar asked Jul 30 '13 16:07

Mike Trpcic


1 Answers

Find the commit hash: git log --pretty=format:"%h %s" | grep "Commit to the hotfix"

Revert the "Uh oh" commit: git revert 15d2e1f -m 2

like image 109
hughes Avatar answered Nov 18 '22 19:11

hughes