0

This is the state of my git repo(u1 means me and u2 is someone else)

C1(U1)->C2(U2)->C3(U2)->C4(U2)->C5(U2)->C6(U2)->C7(U2)->C8(U2)->C9(U2)->C10(U1)

So only C1 is my commit and C10 is actually a conflict merge commit(when I pulled changes from forked repo).

My current HEAD is at C10. I want it to be at C9.

When I do git reset --hard HEAD~1 I see that it actually reverts to C1(my last commit in history), which is not overall last commit.

How do I revert to C9?

edi9999
  • 19,701
  • 13
  • 88
  • 127
rahulserver
  • 10,411
  • 24
  • 90
  • 164

1 Answers1

2

You should write git reset --hard HEAD^2 , which means to access the second parent of the merge commit (which is the one of U2)

See git-revisions

git help revisions

~, e.g. master~3

A suffix ~ to a revision parameter means the commit object that is the <n>th generation
ancestor of the named commit object, following only the first parents. I.e. <rev>~3 is
equivalent to <rev>^^^ which is equivalent to <rev>^1^1^1. See below for an illustration of the usage of this form.

The first parents are, in case of a merge request, the commit that a branch was merged into (here you merged C9 into C1), so C1 is the first parent, and C9 the second parent.

You can access to the nth parent of a commit with commit^n

So if you do

git reset --hard HEAD^2

it should work

Additional info about the ^ notation :

<rev>^, e.g. HEAD^, v1.5.1^0

A suffix ^ to a revision parameter means the first parent of that commit object. ^<n> means
the <n>th parent (i.e. <rev>^ is equivalent to <rev>^1). As a special rule, <rev>^0 means the commit itself and is used when <rev> is the object name of a tag object that refers to a commit object.

edi9999
  • 19,701
  • 13
  • 88
  • 127
  • Does not work. HEAD^2 rollbacks to MY second last commit(i.e. the commit before C1). I want it to be at C9 (which is overall parent of C10) – rahulserver Sep 18 '15 at 15:59
  • Are you sure you use `^` and not `~` ? Can you send a screenshot of `gitk` ? It works fine for me here : http://i.imgur.com/sUcT6TF.png?1 git reset --hard HEAD^2 goes correctly to the "fix issue" commit – edi9999 Sep 18 '15 at 17:11
  • Yes you are right. I was using ~ instead of ^. However that again led me to C9, but I saw that commit C1 (and one before it was lost as I did not push it to remote). I ended up using HEAD~1 and then pulling the code from remote and merging again. – rahulserver Sep 19 '15 at 08:46