3

I made a mistake the other day when doing some commits on a GitHub project for work. I am still a little new to git (an undergrad student) and I need to undo a merge. I accidentally merged the master branch, as seen in the photo below, to the feature branch called 'wav2sig-scal-RMS-vals'. image I've looked at several guides on how to remedy a situation like this, but I can't figure it out. Here is what git reflog returns:

git reflog
9fb9307 HEAD@{0}: undo: checkout: moving from master to wav2sig-scale-RMS-vals
9c3d141 HEAD@{1}: undo: commit: Revert "Merge branch 'master' into wav2sig-scale-RMS-vals"
6933c35 HEAD@{2}: commit: Revert "Merge branch 'master' into wav2sig-scale-RMS-vals"
9c3d141 HEAD@{3}: checkout: moving from master to wav2sig-scale-RMS-vals
9fb9307 HEAD@{4}: clone: from https://github.com/UKY-Distributed-Audio-Lab/Array-Toolbox.git

Any ideas for how to fix this problem? I know it should be simple. Thanks.

1
  • have you pushed the three commits to a remote, or are they still only in your local repo? Commented Sep 8, 2017 at 3:42

2 Answers 2

3

A merge is translated into a commit. All you need to do is to revert to the commit that preceded the merge. use the hashtag of the commit or the HEAD~ as a target for the revert.

look here on how to do a revert.

2
  • Only problem is, I didn't realize the problem and made two commits after the merge, as seen in the image. Is there a way to keep the commits I made in the side branch while separating master from the side branch? Commented Sep 7, 2017 at 15:21
  • 1
    there is no "seperating the master"... merge as I said is a commit at the end of the day. after the commit (merge) there is no more context of what got from master and what got there from the branch so I think you can't achieve what you are asking for. what you can do is to create a new branch from head, revert it to before merge, do git compare with the branch you started with and manually take the changes you want to keep. (If you are using Intellij this can be done quite easily.. probably on similar IDE's as well)
    – Tal Joffe
    Commented Sep 13, 2017 at 7:31
3
  1. git reflog (find the Head of the last git/commit)
  2. git reset --merge 796bf9aee(Head of the last git/commit where you want to reset)

Not the answer you're looking for? Browse other questions tagged or ask your own question.