Before we look at the difference between
HEAD^, let’s look at
HEAD without any modifier.
The most recent commit on the current branch is the
HEAD of the branch. Commands that take a commit as a parameter will use
HEAD by default.
is the same as
git show HEAD
You can also use the at sign (
@) as a shortcut for
git show @
The caret (
^) represents the commit’s parent.
We can display our log in a readable way with
git log --oneline --graph (see How to Improve Git Log).
* d780351 Add LICENSE.txt (HEAD) * 25b2f97 Add plugin code * 300d2a9 Add .gitignore * 20dab1f Initial commit with README.md
Our most recent commit, at the top of the list (
HEAD), is Add LICENSE.txt.
To get our second to last commit, the parent of
HEAD (second from the top) which has the message Add plugin code, we can use
The third commit from the top (the parent of the parent of
HEAD) can be targeted with
Warning: When numbers are used with
^, they do not represent the number of
HEAD^2 is NOT equivalent to
Using a number with
HEAD^2) comes into play when you have a git history that includes merge commits and is a topic for another blog post.
The tilde (
~) is a shortcut character for when you have multiple carets (
^) in a row.
HEAD~3 is equivalent to
HEAD~5 is equivalent to
If no number is specified, the default used is
HEAD~ is equivalent to
When working with git I often use
~ and almost never use
^. Typically, I use
HEAD~2 and when things get more complicated than that, I target the commit by the unique SHA (e.g.
git show 300d2a9).