0

enter image description here

In this case, if you output git log --online, you should see something like this

c6 merge iss53 to master
c5
c3
c4
c2
c1
...

As you can see, c4,c6 are commits from the master branch, and c5,c3 are commits from the iss53 branch

If we want to undo the merge, I would normally do something like this git reset --hard c4

But this is a bit inconvenient... I just want to undo the merge I just did, and I have to find the previous commit of the master branch in the merged logs of the two branches, as shown above (I have to find c4, not c5).
It's not hard, of course, but... I was wondering if there is a better way to do it.

And this is separate from the above question, I have 2 more questions.

1. if I don't do reset as above, and proceed with git reset --hard HEAD~, the merge is canceled fine.
But I'm confused, with the merge in place, isn't HEAD~ c4 and not c5? I thought it would be git reset --hard HEAD~ == git reset --hard c5, but it's not?

2. following on from the above question, what happens if I type git reset --hard c5?
I've tried it myself, but I'm a bit confused as to how it works.

6
  • No need to look for the commit to do the reset. You can simply run git reset --hard HEAD~ if it's the commit you are on that you want to undo. Commented Dec 1, 2023 at 6:11
  • However, as I posted in the question, I don't understand the behavior of git reset --hard HEAD~ - it means to go back to 1 commit before HEAD, which is the same as git reset --hard c5, isn't it? Commented Dec 1, 2023 at 6:13
  • About the first counterfitted question, it really depends on what was the branch that you were on when you did the merge. The branch (actually, commit) that you were on is the first parent (which can be reached with some-commit~ or some-commit^). The branch that you merged in is the second parent (which can be referred to as some-commit^2). Commented Dec 1, 2023 at 6:14
  • 2
    So.... if you did git checkout c4; git merge c5, then @~ will be c4. If you did git checkout c5; git merge c4, then @~ will be c5. Commented Dec 1, 2023 at 6:15
  • 3
    The ~ in HEAD~ means "the first parent" and HEAD~ therefore means "the first parent of HEAD". The merge commit has two parents. Assuming that the merge command was git merge iss53 while you were on branch master (i.e., c4 was the HEAD commit back then), then c4 is the first parent of the merge commit, and c5 is the second parent. Commented Dec 1, 2023 at 7:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.