This tutorial will explain the distinction between both HEAD^ and HEAD~ in Git.
What is the Difference Between HEAD^ and HEAD~ in Git?
In Git, both HEAD^ and HEAD~ refer to the commit immediately preceding the current commit. However, there is little variation or distinction in how they work.
HEAD^
The caret symbol (^) is used to specify the root commit of the current commit. If the user is currently on branch master and runs the Git log, the most recent commit will be shown at the top. HEAD^ refers to the commit just before this HEAD. If they have a merge commit, “HEAD^” specifies the first parent of that merge commit.
HEAD~
The tilde symbol (~) with HEAD specifies the predecessor commits of the current commit. The number after the tilde specifies a particular number of commits you want to skip and move the HEAD pointer back. For example, HEAD~1 refers to one commit before the current commit, HEAD~2 refers to the commit before the current one, and so on.
In most cases, the “^” and “~” syntax can be used interchangeably to refer to the same commit. However, there are some variations when dealing with merge commits. If users have a merge commit with multiple parents, “HEAD^” refers to the first parent, while “HEAD^2” refers to the second parent. The “~” sign does not provide this distinction and only refers to the first parent.
How to Use “HEAD^” and “HEAD~” in Git?
To use the “HEAD^” and “HEAD~” in Git, check out the below-stated steps:
- Redirect to Git local repository.
- View the Git log history of the stated repository.
- Use “HEAD^” to see the output.
- Utilize “HEAD~” with different options.
Step 1: Go to Local Git Directory
Initially, used the “cd” command and set the path of your preferred repository for moving to it:
Step 2: View Git Log History
Execute the “git log” command along with the “–oneline” option to list commits in a single line:
The below-provided image shows that all the commits have been listed successfully:
Step 3: Use “git log” Along With “HEAD^”
Run the “git log” command along with the “HEAD^”. It will show all commits except the most recent commit where the HEAD is pointed:
The resultant image indicates that the most recent commit has been skipped:
Step 4: View Git Log along “HEAD~”
Use the “HEAD~” and set the limit of the commit that you want to skip along with the “git log” command. In this case, we specified the “5” as a limit of the “HEAD~”:
The below-stated output shows that all the commits have been listed except the first five:
Note: The “HEAD^” and “HEAD~” options can be used with different Git commands for multiple purposes.
We have specified the distinction between both “HEAD^” and “HEAD~” in Git.
Conclusion
The caret symbol (^) is used to specify the root commit of the current commit. If the user is currently on branch master and runs the Git log, the most recent commit will be shown at the top. The tilde symbol (~) with HEAD is used to specify the predecessor commits of the current commit. The number after the tilde specifies how many numbers you want to go back. This guide stated the distinction between both HEAD^ and HEAD~ in Git.