Skip to main content

What is the difference between HEAD~1 and HEAD^?

Short answer

  • HEAD~1 is the previous commit along the chain
  • HEAD^ is the parent of the commit

In regular (non-merge) commits they are the same. The difference appears only with a merge.


HEAD~1 - one step back through history

Notation:

bash
HEAD~1

Means:

  • "take the first parent"
  • "go one commit back"

You can continue:

bash
HEAD~2 HEAD~3

So ~ is the number of steps back.


HEAD^ - the parent of a commit

Notation:

bash
HEAD^

Means:

  • "the first parent of the current commit"

A merge commit has several parents:

bash
HEAD^1 # first parent HEAD^2 # second parent

Where the difference shows up (the key point)

Imagine a merge commit:

C---D (feature) / \ A---B-------M (main)
  • HEADM
  • HEAD^1B (the main branch)
  • HEAD^2D (the feature branch)
  • HEAD~1B (always the first parent)

~ always follows the first line of history, ^ lets you choose which parent to take.


Important takeaway for interviews

  • HEAD~1HEAD^ if the commit is not a merge
  • with a merge:
    • ~ - only the main path
    • ^ - access to specific parents

Short table

NotationWhat it means
HEAD~1one commit back
HEAD~2two commits back
HEAD^first parent
HEAD^2second parent (merge)

Short answer for an interview

HEAD~1 is a move to the previous commit along the main line of history. HEAD^ is the parent of a commit. In regular commits they are the same, but with a merge, ^ lets you pick a specific parent, while ~ always follows the first one.


A frequent mistake

"These are just two ways of writing the same thing" is wrong. They match only in linear history.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.