mmirzadeh
mmirzadeh

Reputation: 7089

fix wrong conflict resolution in git

Ok so I messed up! I was working with a repo that had the following structure

                master
                  |
                  v
A--B--C--D--E--F--G
       \
        \
         H--I
            ^
            |
         feature

I was working on the feature branch and once I was done, I merged into master. This resulted in merge conflicts that I had to fix manually ... which I thought I did correctly. Today, however, my coworker told me that I have broken stuff they have developed in F. These broken part are unrelated to what I have added -- apparently in resolving the conflicts I've deleted a bunch of stuff.

How can I "revert" the repo such that I recover their changes while preserving mine? To make things worse, I have already deleted the feature branch on my local repo and its not been pushed to origin. This is what the repo looks like right now

                   master
                     |
                     v
A--B--C--D--E--F--G--J

I have tried

git reset --hard HEAD~
git merge origin/master --no-ff

hoping that would let me manually edit the merged files but it always automatically pulls the latest ...

Upvotes: 4

Views: 7296

Answers (2)

Schleis
Schleis

Reputation: 43780

You can do git cherry-pick <SHA-F> and reapply the changes from that commit.

If the files affected are across multiple commits you can also do

git checkout <SHA> -- <filename>

You can then create one commit that fixes all the files that were broken.

Since you have pushed your changes redoing the merge is not a good option. As you want to avoid rewriting pushed history.

Upvotes: 1

kan
kan

Reputation: 28981

No worries. Nothing vital lost, it is git.

If you merged, you should have a merge commit (a commit with two parents). Are you sure about how does the history look like? Use git log --graph to see the structure of merges.

As you already published the merge to public repo and other developers have it, I don't recommend to use hard resets and rebases.

So, you should find your merge commit and do git revert <sha1ofmerge> --mainline 1 --no-commit, it will modify your local files to undo all changes you made by the merge. Then review the changes, do git checkout <files> for that you want to keep and leave only changes you need to do for other developers make happier. When just git commit.

Having the merge commit, you could restore your branch by git branch feature <sha1ofmerge>^2, here "^2" means second parent, i.e. when you merged feature into master.

Upvotes: 6

Related Questions