rahulserver
rahulserver

Reputation: 11205

Git refer to commit that was merged into the current branch

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?

Upvotes: 0

Views: 39

Answers (1)

edi9999
edi9999

Reputation: 20544

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.

Upvotes: 2

Related Questions