Osama
Osama

Reputation: 476

Is running (git reset head~2) equivalent to running (git reset head^) twice?

If I have two commits on the local branch, and I run git reset head^ twice. Will it just undo the two commits, or will I run into problems?

Upvotes: 1

Views: 759

Answers (2)

Ondrej K.
Ondrej K.

Reputation: 9679

git reset defaults to --mixed and it will indeed undo the commits keeping the changes in your local tree ("the changed files are preserved but not marked for commit").

As for the revision. While in your described scenario you would get the same behavior, you likely want to say: HEAD~ and run it twice... or better yet HEAD~~ or just HEAD~2.

This is not uncommon source of confusion, but in short:

  • ~ traverses through generations (deeper along a line of first parents)
  • ^ traverses across parents of previous generation (mnemonic: circumflex looks like a little fork, you can visit nodes where history forks).

As long as you use them without any numbers, you always get leftmost ancestor with each jump, so HEAD~~~ and HEAD^^^ would land you at the same place, HEAD~2 (two leftmost parents back) is not the same as HEAD^2 (second from the left parent).

You can see more detail in git rev-parse description including this visual illustration:

Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right.

G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
         A

A =      = A^0
B = A^   = A^1     = A~1
C = A^2  = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2

Also, if you are concerned about performing an action, you can always branch off (e.g. git checkout -b temp), try your command out, and when happy switch back, delete the temp branch and execute it there.

Upvotes: 2

Simon
Simon

Reputation: 6490

Yes it's the same thing and you will just undo the two commits (but keeping the changes).

Note that you could also run git reset HEAD^^

Upvotes: 1

Related Questions