0

I have accidentally merged the wrong branch into master, and pushed to origin.

The problem is, that i have resetted the masterbranch to the last before commit-hash with git reset --hard xxx and also the origin with git push origin master --force but now the merge-commit is always there.

The better solution is to use git revert -m 1 <merge-commit>, but I have not :/

Now, from this state, how can i undo the merge an clear the history? Maybe, merge again and revert!?

git history

ninchen
  • 71
  • 8

2 Answers2

1

you are doing

git log --oneline

see the commits before the merge

then just reverting to that commit before merging WITHOUT any reseting!!! After push again to origin as a new commit.

Reverting means that your next snapshot would be equally the same as the one before merging, but your mistaken merge-commit will be saved in overall commits history - which is better to leave there. Avoid git reset hard as much as possible.

  • i am always at the commit before merging, because i have done a reset. How can i do, what you describe. – ninchen Jun 03 '15 at 20:32
  • In that case just git clone your origin to new directory. Perform git revert to the commit without a fault merge on that new local repository. After finally push your new local repo to the origin. –  Jun 03 '15 at 20:34
  • what can happen, when i leave the commit as it is? – ninchen Jun 03 '15 at 20:36
  • Well currently your local branch is one commit behind the origin. You can also do the git pull - which will simply update your current local branch to the state with a false commit. After you can perform git revert to the snapshot without fault-merge. –  Jun 03 '15 at 20:50
  • 1
    So your actions: git pull (updates your local branch), after gir log --oneline (see the id of commit before merge), git revert COMMIT-ID (makes new commit after the fault one), git push origin master (updates your remote repository). Also it takes about a day to learn using git with confidence. After you would solve the kind of questions you are having on ease. –  Jun 03 '15 at 20:57
  • your right. i have to read more about git. i ve done it, as you discribed it. the faulty-merge stays where it is. thanks – ninchen Jun 03 '15 at 21:17
0

There's no need to revert anything here. You almost had the right solution, but your reset to the incorrect commit. The way to fix this is to run:

git reset --hard <pre-merge commit hash>

This will reset your master branch to the commit prior to the merge. From here, you can run:

git push origin master --force

to reset your remote master branch as you had done before. The command that you ran erroneously pointed the master branch to the merge commit hash.

For your own background knowledge, if you take a look in .git/refs/heads you'll see a list of files with the same names as your branches. The contents of these files are just the hashes of each of the commits that they point to. And that's literally all a branch is: a reference to a commit.

When you ran git reset --hard <merge commit hash>, you made the master branch (which is just this very simple reference file) point to the merge commit. By pointing it to the commit before the merge, you'll update this simple master branch reference to point to the hash of the commit before the merge.

Byte Lab
  • 1,576
  • 2
  • 17
  • 42
  • I've done it as you describe it, before i wrote my question. But what is with the history? Do you mean it isnt a problem to let the merge commit there? – ninchen Jun 03 '15 at 18:45
  • I'm not sure I follow. You tried hard resetting to a commit earlier in your history than the merge commit and it's not pointing your branch there? You should not leave the merge commit there. – Byte Lab Jun 03 '15 at 18:48
  • The hard reset is ok. The master is one commit before the merge commit. Now i will undo the merge commit. – ninchen Jun 03 '15 at 18:54
  • What do you mean "undo the merge commit"? If master is pointing at the commit before the merge commit then you've solved your problem. – Byte Lab Jun 03 '15 at 18:55
  • I will delete or revert the merge commit. I will not have it there. – ninchen Jun 03 '15 at 19:02
  • I'm sorry, but you're not explaining yourself clearly. If master is pointing to the commit before the merge commit then you don't need to get rid of it.... it's already gone. – Byte Lab Jun 03 '15 at 19:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79568/discussion-between-decave-and-ninchen). – Byte Lab Jun 03 '15 at 19:04
  • Hmm!? But when its ok, why do you write "You should not leave the merge commit there." – ninchen Jun 03 '15 at 19:08