tags:

views:

168

answers:

4

When I specify an ancestor commit object in Git, I'm confused between HEAD^ and HEAD~.

Both have a "numbered" version like HEAD^3 and HEAD~2.

They seem very similar or the same to me, but are there any difference?

+2  A: 

HEAD^^^ is the same as HEAD~3, selecting the third commit before HEAD

HEAD^2 specifies the second head in a merge commit

knittl
+2  A: 

The ^<n> format allows you to select the nth parent of the commit (relevant in merges). The ~<n> format allows you to select the nth ancestor commit, always following the first parent. See git-rev-parse's documentation for some examples.

jamessan
+1  A: 
  • HEAD~ specifies the first parent on a "branch"

  • HEAD^ allows u to select a specific parent of the commit

An Example:

If you want to follow a side branch, you have to specify something like

master~209^2~15
Diego Dias
+2  A: 

HEAD^ means the first parent of the tip of the current branch.

Remember that git commits can have more than one parent. HEAD^ is short for HEAD^1, and you can also address HEAD^2 and so on as appropriate.

You can get to parents of any commit, not just HEAD. You can also move back through generations: for example, master~2 means the grandparent of the tip of the master branch, favoring the first parent in cases of ambiguity. These specifiers can be chained arbitrarily , e.g., topic~3^2.

For the full details, see "Specifying Revisions" in the git rev-parse documentation.

Greg Bacon