0

How can I merge a state before wrong commits into the current head in Git?

Mistakenly merge by a other person resetted my change on the server, after I fetched and merged the remote master branch, my changes are locally gone. In the meantime are more correct commits are pushed to the remote server and stay on the current head of the master branch.

I need to merge the state before the wrong merge occured into my current master head.

How can I achive that?

I have tried the answer by @Drecker.

git revert -m 2 <SHA_hash_of_merge_commit>

and I have now my correct file content back, but I loose the correct file content from the second person. I committed it, but don't pushed it. I created a new branch restore from that. Now the second person content is correct in the remote master branch and my content is correct in the local restore branch.

How can I re-merge both branches again, so that I have the correct contents from both branches?

Community
  • 1
  • 1
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96

2 Answers2

0

UPDATE: you may read this post: Undo a Git merge that hasn't been pushed yet

To "undo" a merge that has been committed (and pushed) you may use

git revert -m <parent_number> <SHA_hash_of_merge_commit>

the parent_number is indexed number of parents of that merge - it is required because git wouldn't know which branch you want to reject (indexed from 1). This way a new commit will be created deleting all changes that was created by rejected branch

Other (unrecomanded) option is to use

git rebase -i <SHA_of_commit_to_which_you_want_to_change_histrory>

this way you will interactively choose which commit you want in current HEAD and which not

WARNING: this way you actually change git history! You may lose some changes (that wasn't at any other branch) and cause more problems when your coworkers will pull changes

Community
  • 1
  • 1
Drecker
  • 1,215
  • 10
  • 24
0

You need to recover the state before the first merge.

You can acchive it by using git reflog + git reset, this way (I am assuming the branch is master):

git reflog master

Will return the history of your local master ref, is an output similar to this:

1904ece master@{0}: commit: fixed styles
ba185a3 master@{1}: commit: fade transition for angular pages|
b06d0a5 master@{2}: commit: fixed detail view
792af52 master@{3}: commit (merge): Merge branch 'origin/master' into master
128dd58 master@{4}: commit: fixed save of searchs
...

Note the merge commit, noted with "commit (merge)", maybe of those is the merge commit you are trying to "undo", to proceed you can use git reset command, this way:

git stash # save all local changes on index and WC just in case
git reset --hard master@{4}

Note I am using master@{4} here, because merge commit was found as master@{3}

After doing this, you will need to pull and do the merge again, you can do this as many times as you need to re-do merge operations

dseminara
  • 11,665
  • 2
  • 20
  • 22
  • If I do that, I get the wrong content back, if I merge master from remote into my local resetted state. – BuZZ-dEE Sep 12 '14 at 15:05
  • I will add more details to the answer, I explained how you can redo the merge, but you need to know how to "merge the state" (what are you mean with "state"?) before merge remote origin/master – dseminara Sep 12 '14 at 15:08
  • I mean the content of files, that I have committed / pushed and are accidently overridden by a merge commit / push by a second person. That is the correct state of my files. – BuZZ-dEE Sep 12 '14 at 15:12