In Git, the HEAD
is the current state of the branch you’re working in. The HEAD
is a pointer that follows you everywhere you go.
If you’ve checked out in any git branch and run git show HEAD, it shows the most recent commit.
$ git show HEADcommit 8994913e0be924ac5330bf85a35ecb08983731ce (HEAD -> main)Merge: 59beccd 8ee00b3Author: artturijalliDate: Tue Dec 6 13:19:13 2022 +0200 Merge branch 'feature'
In a sense, the Git HEAD is you. It points to the tip of your current branch and moves with the branch as you make new commits.
The HEAD
can also point to a specific commit instead of a branch. When this happens, you’re in a detached HEAD
state.
This guide teaches you what HEAD is in Git, and what kinds of different HEAD notations (HEAD~ HEAD^) you might encounter and how they work.
Let’s jump into it!
What Is HEAD in Git?

In Git, the HEAD
is a symbolic reference to where you’re at right now. The HEAD follows you everywhere you go in Git.
If you’ve checked out to a branch, the HEAD
references the state of the branch you’re in. In other words, HEAD
leads you to the most recent commit in the branch. If you make a new commit to the branch, the HEAD and the branch reference will move together to point to the new commit.
Most tutorials (including this one) say that the HEAD
points to the most recent commit in a branch. But in reality, the HEAD
references the current branch instead of a particular commit.
If this confuses you, think about it this way: The state of the current branch is what the most recent commit produced. The HEAD
, which is a reference to the current branch, is a pointer to this most recent commit, even though it technically points to the branch, not to the commit.
Example
You can check where your head points by running cat .git/HEAD
.
This lists the contents of the HEAD
file, that is, where the HEAD
currently points at.
For example, I have an example project and I’m in the main branch. This is what the cat .git/HEAD
command shows me:

This shows that the HEAD
indeed points to the main
branch.
Detached HEAD in Git
Previously, you learned that the HEAD
points to the branch you’re currently in.
But it is possible to make the HEAD
point to a specific commit in the past. More specifically, if you checkout to a particular commit, you’re detaching the HEAD
from the branch and making it point to a commit. This is known as the detached head state in Git.
To further demonstrate, if I now check out to one of the previous commits in the main branch, the HEAD
will point to the checked-out commit instead of the branch.
$ git checkout accf83fb43aabd0f55780657d57e07ffbeeeb1e1Note: switching to 'accf83fb43aabd0f55780657d57e07ffbeeeb1e1'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by switching back to a branch.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name>Or undo this operation with: git switch -Turn off this advice by setting config variable advice.detachedHead to falseHEAD is now at accf83f Change the file contents
From the rather long message above you can see that the HEAD
is now pointing to a commit instead of the main branch.
Let’s verify this with cat .git/HEAD
:

As you can see, now the HEAD
points to a commit instead of a branch. In this restricted mode, you cannot do much—staging and committing isn’t possible because the HEAD
doesn’t point to a branch but a commit instead.
The HEAD Is Always You in Git
In Git, the HEAD is you. It follows you everywhere. You cannot lose the HEAD in Git.
Even if you are in the detached HEAD state, you haven’t lost the HEAD. Instead, the HEAD has followed you and now points to a commit rather than to a branch.
Now that you have a better understanding of HEAD in Git, let’s take a look at some notations you may have come across.
Different HEAD Notations You See in Git
Have you wondered what’s the significance of the notations such as HEAD~
, HEAD^^^
, HEAD@{2}
?
This section teaches you what the following HEAD references mean and how they work in Git:
- HEAD~ and HEAD~N
- HEAD^ and HEAD^N
- HEAD@{N}
1. What Is HEAD~?
HEAD~
is a shortcut that refers to the commit that took place before the most recent commit. In a sense, the HEAD~
is the state of the branch one commit before the HEAD
commit.
You can view the commit before teh most recent commit by running:
$ git show HEAD~
Using the HEAD~
notation is useful when you need to reference commits but don’t want to look up the commit IDs.
For example, you can check the changes between the 2nd and 3rd most recent commits by:
$ git diff HEAD~ HEAD~~
You can also specify a number after the ~ character to reference a specific number of commits before the HEAD
commit.
$ git show HEAD~N
For example, HEAD~2
would reference the commit that is two commits before the HEAD
commit (i.e. HEAD~~
), and HEAD~3
would reference the commit that is three commits before the HEAD
commit (i.e. HEAD~~
~).
Here’s an illustration of how the HEAD~ notation works:

2. What Is HEAD^?
HEAD^
is another shortcut that refers to a specific commit in Git. It is very similar to HEAD~
. The HEAD^
thus points to the commit that was made before the HEAD
commit, on the same branch.
Based on this description, it seems as if HEAD^
and HEAD~
worked the exact same way.
The similarity between HEAD^
and HEAD~
is that you can place consecutive carets or tildes to reference earlier commits.
For example:
HEAD^^^
is the third most recent commit.HEAD~~~
is the third most recent commit.
But there’s also a clear difference between HEAD^
and HEAD~
in Git.
You can call HEAD^
followed by a number, such as HEAD^2
but it’s not the same as HEAD^^
! (With HEAD~ notation, HEAD~~
is the same as HEAD~2
).
HEAD^1
would reference the first parent of the HEAD
commit, HEAD^2
would reference the second parent of the HEAD
commit (if it exists), and so on.
For example, if your branch is not merged with another branch, the HEAD^2
doesn’t exist.
The number notation with HEAD^
comes into play if you have merged a branch with the current branch. This image illustrates the use of HEAD^
perfectly. The most recent commit, D
, is the merge commit of main
and feature branch
.

So as you can see, the HEAD^1
refers to the most recent commit made in the main
branch. The HEAD^2
refers to the most recent commit made in the feature
branch before merging. If there was no feature
branch merged to the main branch, HEAD^2
wouldn’t exist.
3. What Is HEAD@{N}?
HEAD@{N}
is a reference to a specific state of the HEAD
pointer in the Git reflog.
In case you didn’t know the git reflog is a reference log that stores all the changes that have been made to the HEAD
pointer in a local Git repository.
Each entry in the reflog has a corresponding number, starting at 0
for the most recent entry, and incrementing by 1
for each earlier entry. HEAD@{N}
refers to the state of the HEAD
pointer at the N
th entry in the reflog.
The git reflog is a valuable tool for managing and tracking Git operations performed in a local repository. It allows you to see a log of all the changes that have been made to the HEAD
pointer, which can be useful for identifying when certain commits were made. More importantly, it allows for recovering from mistakes or unintended changes to the repository.
One common use for the reflog is to recover from mistakes, such as undoing a hard reset. With the reflog, you can find the state of the HEAD
pointer at the time before you made the mistake to restore the repository to that state.
Summary
Today you learned what is the HEAD
in Git.
To take home, the HEAD
is a reference to the current state of the local repository. It points to the most recent commit on the branch that is currently checked out. The HEAD
can be thought of as a pointer that indicates the current position of the repository.
A great way to view the HEAD
is that it’s essentially you! Whatever you do on Git, the HEAD
follows. If you create a new commit, the HEAD
(and the branch) moves along. If you checkout to a commit in the past, the HEAD
follows. The HEAD
always points to where you are in the project.
You can reference earlier commits in the branch by adding tildes after HEAD
. For example, HEAD~~
refers to two commits before the most recent one.
Thanks for reading. Happy coding!
Read Also
How to Undo Git Reset
About the Author
- Artturi Jalli
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
- Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
- Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
- Artificial Intelligence2023.04.11
- JavaScript2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)
FAQs
What is the head of a git file? ›
When working with Git, only one branch can be checked out at a time - and this is what's called the "HEAD" branch. Often, this is also referred to as the "active" or "current" branch. Git makes note of this current branch in a file located inside the Git repository, in . git/HEAD .
What does the head represent in git? ›The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory. That also means it will be the parent of the next commit you do.
How do you check what is the head of a git? ›The git show head is used to check the status of the Head. This command will show the location of the Head. Syntax: $ git show HEAD.
What is the meaning of head of branch? ›Head of Branch means an officer declared as such under any general or special orders of the competent authority. The term includes an Incharge of a Branch.
What is head vs master branch git? ›The simple answer is that HEAD is a pointer/label to the most recent commit of the branch you are currently on. master is the default branch created when you initialized a git repository (e.g. git init ). You can delete the master branch (e.g. git branch -D master ). You cannot delete the HEAD pointer.
What is head vs base git? ›Base: Base is the repository that will be updated. Changes will be added to this repository via the pull request. Following the example above, the base repo is your colleague's repo. Head: Head is the repository containing the changes that will be added to the base.
How many heads can be created in git? ›While there can be as many heads there are branches, there can be only one HEAD, since you cannot be at multiple places at once. Unless you count git worktree list , which would list multiple working trees attached to the same repository... each with their own HEAD!
What is the difference between git head and checkout? ›Remember that the HEAD is Git's way of referring to the current snapshot. Internally, the git checkout command simply updates the HEAD to point to either the specified branch or commit. When it points to a branch, Git doesn't complain, but when you check out a commit, it switches into a “detached HEAD” state.
What is the difference between head and working tree in git? ›Your working tree is what is actually in the files that you are currently working on. HEAD is a pointer to the branch or commit that you last checked out, and which will be the parent of a new commit if you make it.
What does head 2 mean? ›HEAD^2 refers to the commit's second parent. A commit can have two parents in a merge commit.
How to compare head to branch git? ›
In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.
What is origin master and origin head? ›HEAD is not the latest revision, it's the current revision. Usually, it's the latest revision of the current branch, but it doesn't have to be. master is a name commonly given to the main branch, but it could be called anything else (or there could be no main branch). origin is a name commonly given to the main remote.
What is head and base branch? ›Base Branch: The "Base Branch" is the branch where your new changes should be applied / integrated into. Head Branch: The "Head Branch" is the branch that contains the changes you want to integrate.
What is origin head? ›It represents the default branch on a remote and is a local ref representing a local copy of the HEAD in the remote repository. In summary, origin/HEAD represents the default branch on the remote, which is defined automatically when you clone a repository from the internet.
What is the difference between head branch and base branch? ›The terms "head" and "base" are used as they normally are in Git. The head is the branch which you are on; that is, the branch with your changes. The base is the branch off which these changes are based. This is similar to the terminology used for git rebase and git merge-base .
What are the three types of branching in git? ›There are three types of supporting branches with different intended purposes: feature, release, and hotfix.
What is the difference between git merge and rebase head? ›The Workings of Git Rebase and Merge
In the process, rebase flattens the history, removing unwanted entries. Git merge, on the other hand, only changes the target branch and creates a commit, preserving the history of the source branch.
- git branch.
- * master.
- git checkout master.
- Switched to branch 'master'
Detaching the HEAD
There are a couple of ways we can detach our HEAD. Using the git checkout --detach command. By adding ^0 on any given branch. E.g. git checkout master^0 .
To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
How to merge 2 branches into 1 git? ›
To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.
What is the largest git file? ›File size limits
GitHub limits the size of files allowed in repositories. If you attempt to add or update a file that is larger than 50 MB, you will receive a warning from Git.
HEAD : The current reference point in a git log. HEAD~ : shorthand for HEAD~1 . It means reference HEAD 's first parent. HEAD~2 : means reference HEAD 's grandparent, or first parent's parent. HEAD~3 : means reference HEAD 's great-grandparent, or first parent's parent's parent.
Can we checkout two branches in git? ›You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.
What is the difference between head and head 1 in git? ›Git HEAD~1 means the previous commit of the last commit. Contrary to using the caret, git HEAD~ or HEAD with a tilde is simpler to understand as it references the previous commit of a specific branch. Another way to think about this is to go backward in a straightline.
What is the difference between head and index? ›HEAD means what's in the [current] branch. > The index is what's in the index., whatever you've added to the staging area (but uncommitted). If there's nothing in the staging area then it's just going to compare with HEAD I believe.
What is the difference between git reset head and git restore? ›" reset is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history." " restore is about restoring files in the working tree from either the index or another commit. This command does not update your branch.
What is the difference between trunk and branch in git? ›In software development, a trunk is the base code into which all subsequent code is merged. Copies made from the source code are known as branches, extending outward from the trunk.
What is head and head 1? ›HEAD~ or HEAD~1 refers to the first commit before the latest commit. HEAD~2 refers to the second commit before the latest commit. HEAD~3 refers to the third commit before the latest commit. And so on.
How do you reset head 1? ›The command `git reset HEAD~1` can be used to reset the Git Head to the commit right before the previous one, making any changes in that commit available for modifications. If you want to discard any changes made after this commit, use `git reset –hard HEAD~1`, which will permanently delete them from the Git history.
How do I switch branches without losing changes in git? ›
The Git Stash Command and How to Force a Branch Switch
To commit your changes, you can use the git commit command. To stash your changes, you can use the git stash command. This will temporarily store your changes, allowing you to switch to a different branch without committing or discarding your changes.
To compare any two commits in your branch, use the Ctrl key to select the two commits that you want to compare. Then, right-click one of them and select Compare Commits.
How to view difference between local master and remote master? ›1 Answer. You can use git branch -a to list all branches then choose the branch name from the list from the remote branch name. Example: git diff master origin/master (where "master" is a local master branch and "origin/master" is a remote namely origin and master branch.)
What are the git commands? ›- git add. Moves changes from the working directory to the staging area. ...
- git branch. This command is your general-purpose branch administration tool. ...
- git checkout. ...
- git clean. ...
- git clone. ...
- git commit. ...
- git commit --amend. ...
- git config.
Simply double-click a branch in the sidebar to make it the new HEAD branch - or choose a branch from a list.
What is the difference between master and head? ›The master itself is a pointer to the latest commit. The HEAD is a reference that points to the master. Every time you commit, Git updates both master and the HEAD pointers to point to the last commit by default.
How do I set origin to head? ›origin/HEAD is set automatically when you clone a repository, and that's about it. Bizarrely, that it's not set by commands like git remote update - I believe the only way it will change is if you manually change it.
Can head point to a branch? ›The HEAD will point to the tip of a branch by default, but can be set to point to any commit. When pointed at a specific commit the HEAD is considered to be in a 'detached' state.
How do you find the head of a branch? ›In Git, a head is a ref that points to the tip (latest commit) of a branch. You can view your repository's heads in the path . git/refs/heads/ . In this path you will find one file for each branch, and the content in each file will be the commit ID of the tip (most recent commit) of that branch.
What is a repository without a working tree called? ›A bare Git repository is a repository that is created without a Working Tree. Go ahead and create one to see. $ git init --bare . Run ls on that directory and you won't see a Working Tree but just the contents of what is typically in the . git directory.
What is the difference between git master and origin master? ›
Master: This is a branch name where we first initiate git and then we use to make commits. And the changes in the master can pull/push into a remote. origin/master: This is a remote branch, which has a local branch named master on a remote named origin.
What is the difference between main and origin in git? ›main is a local branch. origin/main is a remote tracking branch (which is a local copy of the branch named "main" on the remote named "origin")
What is the difference between origin master and origin master? ›To answer your question, the git pull origin master will pull changes from the origin remote, master branch and merge them to the locally checked-out branch. However, the git pull origin/master will pull changes from the locally stored branch origin/master and merge that to the local checked-out branch.
What is the difference between main and head? ›<head> tag is where you place tags such as “title”, “meta”. It goes right before <body> tag. Whereas <main> tag goes between <body></body> tags. <main> tag is a “semantic” HTML element.
What is a detached head state in Git? ›A detached HEAD occurs when you check out a commit that is not a branch. The term detached HEAD tells you that you are not viewing the HEAD of any repository. The HEAD is the most recent version of a branch. This is sometimes called the “tip of a branch”.
What is a rebase in Git? ›What is git rebase? Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow.
What does head 1 mean? ›HEAD^1 refers to the commit's first parent. HEAD^2 refers to the commit's second parent. A commit can have two parents in a merge commit.
What is the difference between Git head and checkout? ›Remember that the HEAD is Git's way of referring to the current snapshot. Internally, the git checkout command simply updates the HEAD to point to either the specified branch or commit. When it points to a branch, Git doesn't complain, but when you check out a commit, it switches into a “detached HEAD” state.
What are the 3 main states of a file in Git? ›Git has three main states that your files can reside in: modified, staged, and committed: Modified means that you have changed the file but have not committed it to your database yet.
How to reset HEAD for file in Git? ›To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
How to check difference between two branches in git? ›
- Use the git diff command to view the differences between two branches in a Git repository. git diff branch1 branch2 will show all the differences. ...
- The git log command can also be used to view the differences between two branches.
- Initiating Git. git init. ...
- Viewing Your Staged and Unstaged Changes. After the git add, the staged files are ready to be committed. ...
- Committing the changes. ...
- Adding shortname for the remote repository. ...
- Pushing the committed code to Git Repository.
The size is simply the size of the contents, the content depends on what type of object is, and there are four different types of objects: “blob”, “tree”, “commit”, and “tag”. Git stores these different types of objects in .
What are the four different types of git objects? ›Git places only four types of objects in the object store: the blobs, trees, commits, and tags. These four atomic objects form the foundation of Git's higher level data structures. Each version of a file is represented as a blob.
How do you reset head by 1 commit? ›The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.
How do you fix a detached head? ›To save changes committed in a detached HEAD state, you first need to create a new branch. Continuing from the scenario described above, you create a new branch called temp-branch . As soon as you make the branch and check out to it, the HEAD is no longer detached.
What does git head reset do? ›The git reset HEAD~2 command moves the current branch backward by two commits, effectively removing the two snapshots we just created from the project history. Remember that this kind of reset should only be used on unpublished commits.