Git HEAD^ vs Git HEAD~ vs Git HEAD{@}
This article will discuss the difference between a Git caret and a Git tilde. A Git caret is represented by ^
while a tilde is represented by ~
.
We will also discuss the HEAD{@}
. Let’s start.
Git HEAD^
(Caret)
When introducing a caret to Git HEAD
, we point to the commit’s first parent. Hence git HEAD^2
points to the commit’s second parent.
What is a commit’s parent?
A commit’s parent comes into play when we merge one or more branches with another. Let’s look at an example.
Our repo has the Master
branch in green and the Other-Branch
branch in yellow.
Our commit E
on the other-branch
is merged with the commit A
on the master
branch. As such, we can say the commit A
has two parent commits.
Hence, a parent commit is simply the previous commit of a commit.
Look at the commands below.
$ git show HEAD^ --oneline
Running the command above will move our HEAD
at commit B
if we are at commit A
.
$ git show HEAD^2 --oneline
On the other hand, the command above will shift our HEAD
to commit E
.
Note that HEAD^
and HEAD^1
serve the same purpose.
It is worth noting that the above explanation only applies when we have merged branches. If you are working with one branch, then HEAD^
will be the equivalent of HEAD~
.
Git HEAD~
(Tilde)
Adding a tilde to the HEAD
points at the previous commit of the last commit in our branch. It is like going back in a straight line.
Let’s look at an example.
Hence, if our HEAD
is at commit A
, then git HEAD~
or git HEAD~1
will shift us to commit B
. HEAD~2
will shift us to commit C
.
If we are checked out at commit E
in the other-branch
, HEAD~1
will shift us to commit F
and so on.
Git HEAD{@}
This combination points to the position of the reference at a particular time in our local repository. It usually pops up when running a git reflog
command, as shown below.
In a nutshell, a caret is useful when we merge branches on other branches, and we need to switch to a commit in a different branch. The tilde allows us to shift to a previous commit of the last commit in a specific branch.
When working on a single branch, HEAD^
is the same as HEAD~
.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn