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

1

6

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?

Catania answered 30/7, 2013 at 16:3 Comment(0)
D
3

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

Delossantos answered 30/7, 2013 at 16:10 Comment(1)
But there's not just one commit. The "Uh Oh" commit was a merge from another branch, which included a bunch of commits! I'd need to go through the Hotfix history and revert a bunch of commits, which seems a little "hacky"Catania

© 2022 - 2024 — McMap. All rights reserved.