Getting merged code “right” with Git

2020-07-18 04:19发布

问题:

I've just overwritten my co-workers code by merging FETCH_HEAD and getting my more recent changes instead of his. I want to revert to before the merge, then do the merge forcing his changes to be accepted where he touched the file, but getting my merges where there isn't any other changes.

My git log (created with l2*) looks like this now :

*    3f6308d - (HEAD, master) Merging changes (confliect in PriceListForm.java.. was a formatting change only (Sun Dec 29 09:07:27 2013) <Gre
|\  
| *  283c00c - Changing wv reports to be separated by changes in prices according to received_date rather than lab_number. (Thu Dec 26 19:39:
| |
| *  4846bf2 - Merge branch 'master' of ssh://git-pacce@free1.projectlocker.com/pcs.git (Wed Dec 25 17:49:19 2013) <jpjones>

When I performed the merge, there weren't any conflicts in those files, it just took my changes instead of the less recent changes by jpjones.

Basically I want to redo 3f6308d, but allow jpjones changes to take precedence. This StackOverflow Answer seems to be relevant to what I want to achieve, but I'm not sure and was hoping for some clarification.

* git l2 is alias l2 = log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date=local

回答1:

Considering you haven't pushed your faulty commit yet, you can safely reset your HEAD to the commit before:

git reset --hard HEAD^

(Make sure you don't have work in progress, like private files or files added to the index, because both the working tree and the index would be reset to the HEAD^ state)

This is what the answer you mention suggests.

But after that, you need to make sure your coworkers' changes are taken during the merge your are about to do again.
See if a theirs "merge strategy option" works better.

git checkout yourBranch
git merge -X theirs theirBranch

However, I suspect it would work only for solving conflicts (concurrent modifications in a file).
If you have made more recent changes, those would still be in the resulting merged HEAD.

If that is so, try following the recipe in this answer, which forces a merge to your branch, and then reset yourBranch to theirBranch, before moving HEAD to the merged commit done before.
Then end result should look like what you are after.