Git detached head: What is it & How to fix it (2023)

Have you ever paused for a few seconds, staring at your screen with bewilderment, trying to figure out the informational message Git wrote to the console? And how you got to the state Git says you are in? You are not alone! We have been there, and that’s exactly what a git detached HEAD does to you, especially when you encounter it for the first time.

To understand what a git detached HEAD is, let’s first take a look at what a HEAD is in git.

Key Takeaways

  • A git detached HEAD state occurs when you are not on a branch but directly on a specific commit. There are also several ways to get to this state, but the common scenario is checking out to a commit via its hash.
  • You can generally get out of a git detached HEAD state by simply running the git checkout or git switch command.
  • A git detached HEAD has a few useful use cases. For example, you can use it to test out some experimental code logic or even bisect a commit that you think might have introduced some bugs into the project’s codebase.

What is a git HEAD?

A git HEAD is simply a pointer that points to a specific version or state of a git repository. It can point to either the latest commit on a branch or to a specific commit directly.

When you switch or checkout to a branch, the HEAD points to the latest commit made on that branch. It points to the current or active branch reference. Let’s visualize what a HEAD on a git branch looks like below:

Git detached head: What is it & How to fix it (1)

The image above shows we have a master branch and a feature branch. The local repository usually starts out with a master branch. There is also a feature branch we switched or checked out into from the master branch, as seen above. The HEAD points to the active branch reference - the feature_branch in this case - where the latest commit is made.

Hence, when on a branch, the HEAD always points to the latest commit made on that checked-out branch.

On the other hand, when a branch points directly to a specific commit, it puts you in a git detached HEAD state. Let’s dive deeper into what this really means in the following sections.

To learn more about Git’s basic concepts and how to set it up in your system? Check out this video:

(Video) Git: What is Detached Head Scenario & Fix

What is a git detached HEAD?

When you are in a git detached HEAD state, you are not on any branch. The HEAD references or points to a commit directly instead of a branch. Let’s visualize what a git detached HEAD looks like:

Git detached head: What is it & How to fix it (2)

From the image above, we switched or checked out to the commit hash, 935baeb. The HEAD moved from the feature_branch reference to now point directly to the commit with hash 935baeb, which is not on any branch.

You can still create more commits when in a git detached HEAD state. These commits are seen as experimental and can be discarded or saved depending on your scenario. Below is a visualization of experimental commits made in a git detached HEAD state:

Git detached head: What is it & How to fix it (3)

From above, we made extra commits, 7e4b8e9 and 25f7143, in the git detached HEAD state. The HEAD also moved to point to the latest commit made when in this state, as seen above. Please note that all other branches in the example given above, i.e. master and feature_branch, would still exist in your local git repository. The HEAD, which is a pointer just simply moved to now point directly to a commit.

Now, let’s look at some git operations that could land us in a git detached HEAD state.

Git operations that can put you in a git detached HEAD state

You most times get to a git detached HEAD state unintentionally. There are several git operations you could perform that get you in this state. Some of them are highlighted below:

Switching or checking out to a commit via its hash

git switch <commit_hash> or git checkout <commit_hash>

When you switch or check out directly to a commit via its hash, it puts you in a git detached HEAD state. We saw this in the “What is a git detached HEAD” section.

(Video) 21. Detached Head in GIT. How can we handle the detached head state in Project - GIT

Switching or checking out to a relative commit reference

git switch <relative_commit_reference> or git checkout <relative_commit_reference> 

Another operation that can lead us to a detached HEAD state is switching directly to a specific commit using a relative commit reference git checkout HEAD~2. This switches or checks out to the 2nd parent commit i.e. the commit that is 2 parents behind the current commit. Let’s see an example below for you to understand what a relative commit reference looks like:

Git detached head: What is it & How to fix it (4)

Currently, the HEAD is at 22782ac. So, when you run the command git checkout HEAD~2, the HEAD will now point to b7b2559 as seen below:

Git detached head: What is it & How to fix it (5)

So, switching to a commit using a relative commit reference also puts you in a git detached HEAD state.

Switching or checking out to a tag reference

git switch <tag_name> or git checkout <tag_name>

Switching to a tag also puts you in a git detached HEAD state since a git tag references or marks a specific commit.

Switching or checking out to a remote branch without creating a local branch to track it

git switch <remote_repo_alias/branch_name> or git checkout <remote_repo_alias/branch_name> 

If you checkout directly to a remote branch without creating a local branch on your repository to track it, Git will put you in a git detached HEAD state.

Git rebase with merge conflicts

Also, when you perform git rebase <branch_name> but end up with merge conflicts, Git can put you in a git detached HEAD state.

Git rebase, like git merge, is used to get changes from one branch into another branch but unlike git merge, it rewrites the commit history by creating new commit hashes. So, a git rebase operation basically involves moving commits. When Git gets confused about which commit to apply, you could end up in a git detached HEAD state.

(Video) Learn Git Essentials 9: Head & Detached Head

There are several scenarios that could put you in such a state. Feel free to share more examples experienced in the comment section below.

Now, let’s jump into how we can exit the git detached HEAD state.

How to fix a git detached HEAD

Most times, you unintentionally get into this state and will want to get out of this state. Other times, you get into this state intentionally to try out some things (experimentation). These are 2 possible scenarios you may find yourself in. Now, let’s assume you are currently in the git detached HEAD state seen below:

Git detached head: What is it & How to fix it (6)

Let’s see how to go about exiting this state for both scenarios mentioned above.

Fix a git detached HEAD if you got there unintentionally

If you got to this state accidentally, you would want to get out as soon as possible, probably disregarding any accidental code changes made too.

To do this, just switch or check out to any of your existing branches on your local git repository. You can do so by running the git command below:

git checkout feature_branch or git switch feature_branch

When you run any of the commands above, the HEAD will now point to the branch reference, feature_branch, as seen below:

(Video) Using Git: What is a "Detached HEAD"?

Git detached head: What is it & How to fix it (7)

During the garbage collection process, Git prunes or deletes those extra commits, i.e. 7e4b8e9 and 25f7143. This helps in reclaiming disk space, which improves the repository’s performance, especially for large git repositories.

Fix a git detached HEAD if you got there intentionally

You could also want to use a git detached HEAD state to test out some code changes that you may eventually commit.

To get out of this state but still keep these code changes, you can just create a new branch and switch to it and then add and commit your changes. You can do so by running either of the commands below:

# creates a new branch and then switches to itgit switch -c feature_test_branch or git checkout -b feature_test_branch# stages your code changesgit add . # saves your code changes in your local git repositorygit commit -m “Add a commit message here” 

Alternatively, you can run git branch feature_test_branch to create the feature_test_branch, followed by git checkout feature_test_branch or git switch feature_test_branch to switch or check out to feature_test_branch, followed by the git commands to stage and save your code changes.

Use cases for a git detached HEAD

There are several use cases for this state. A few are highlighted below:

  • You can time travel. Yeah! You really can but only in your git repository in this case. Being in a git detached HEAD state allows you to explore any commit in your commit history.
  • You can run code change experiments. You can test out any desired code changes on a specific commit without worrying whether it will affect any branch.
  • You can also debug issues when in this state using the git bisect command to find the commit that introduced a bug.


Below is a frequently asked question about git detached HEAD:

Are there any best practices to avoid a git detached HEAD?

If you want to avoid the git detached HEAD state, do the following:

(Video) Lesson - 2.6. Git Detached HEAD (What is it and how to fix it)

  • Always check out a branch reference. Avoid switching directly to a commit, tag, or remote branch.
  • Use git rebase cautiously. If a merge conflict occurs during a rebase operation, Git can pause the rebase operation and put you in a detached state.


Now, you understand what the git detached HEAD is and won’t be confused when you encounter it again in the future. The git detached HEAD state can be very useful if you understand what it is and how it works. But if you want to get out of that state, you can follow the steps explained in this piece to fix it.
Are you interested in learning Git?

Check out our Git for Beginners Course, which covers the fundamentals you need to get up and running with Git. It also includes simple visualizations, animations, and hands-on labs to help you internalize concepts, git commands, and also work with Git as you would in a real-world environment.


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.

How do I push changes from detached head to branch? ›

Correcting detached head problems with Git¶
  1. From a command prompt window, create a branch by using a command similar to the following: git checkout -b [branchname]
  2. Commit your changes to the branch.
  3. Push the branch that you created into the origin Git repository: git push origin [branchname]

How do I merge detached head to main? ›

1 Answer
  1. If you've made some commits in the detached head then if you need those commits on your master. For that, all you need is to create a new branch and merge it to master and then delete the branch. For that you can do: git branch temp.
  2. Now checkout to master. git checkout master.
  3. Merge the branch. git merge temp.
Jul 24, 2019

What does detached head mean? ›

When the HEAD pointer is moved from its default position, we get a warning “detached HEAD state”. This simply means that HEAD is not pointing to any branch, rather it now points to a specific commit. In other words, if the HEAD points to a specific commit, it is said to be detached.

How to merge two git branches? ›

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 happens when you push a detached head? ›

Detached head usually means that the branch you checkout to does not has the latest commit. So, basically you need to adjust the HEAD of your current branch to the latest commit.

How do you solve head detached at origin master? ›

Steps to reconciling the detached HEAD with master/origin
  1. git branch temp git checkout temp. ...
  2. git checkout -b temp. ...
  3. git log --graph --decorate --pretty=oneline --abbrev-commit master origin/master temp git diff master temp git diff origin/master temp. ...
  4. git branch -f master temp git checkout master. ...
  5. git checkout -B master temp.

How do I force push a branch? ›

To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details. Force an update only if the tip of the remote-tracking ref has been integrated locally.

How do I find a detached commit? ›

You can find your missing commit using the git reflog command. The reflog keeps track of the historical positions of a branch head, and you can use it to find things that the branch head was pointing at previously.

How to resolve merge conflicts in git? ›

Resolving a merge conflict on GitHub
  1. Under your repository name, click Pull requests.
  2. In the "Pull Requests" list, click the pull request with a merge conflict that you'd like to resolve.
  3. Near the bottom of your pull request, click Resolve conflicts.

How do I change my head to another branch? ›

git switch
  1. The "switch" command allows you to switch your current HEAD branch. ...
  2. The name of a local or remote branch that you want to switch to. ...
  3. The name of a new local branch you want to create. ...
  4. Switch to the specified branch and discard any local changes to obtain a clean working copy.

How do I merge head to master branch? ›

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.

How do I delete a detached head branch? ›

Deletes all detached branches. Use git fetch --all --prune to garbage collect any detached branches. This is especially useful if the remote repository is set to automatically delete merged branches.

What is the difference between git fetch and pull? ›

Difference between Git fetch and pull. The key difference between git fetch and pull is that git pull copies changes from a remote repository directly into your working directory, while git fetch does not. The git fetch command only copies changes into your local Git repo. The git pull command does both.

How to revert code in git? ›

The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset , move the HEAD and branch ref pointers to a specified commit. Git revert also takes a specified commit, however, git revert does not move ref pointers to this commit.

How do I merge two branches without pull request? ›

1 comment
  1. move to “branch”: git checkout branch.
  2. merge “master” in “branch”: git merge master.
Jan 12, 2019

How to merge two files in git? ›

This code example executes a sequence of commands that accomplish the following.
  1. Create a new directory named git-merge-test, change to that directory, and initialize it as a new Git repo.
  2. Create a new text file merge.txt with some content in it.
  3. Add merge.txt to the repo and commit it.

What is rebase instead of merge? ›

Merge lets you merge different Git branches. Rebase allows you to integrate the changes from one branch into another. Merge logs show you the complete history of commit merging. Rebase logs are linear. As the commits are rebased, the history is altered to reflect this.

How do I know if my head is detached? ›

If you check out to the origin (main) branch, which is read-only, you will get notified that you are in the detached HEAD state. There are other scenarios as well. For instance, checking out to a specific tag name or adding ^0 on any given branch will result in Git detached HEAD state.

What is the difference between master and head in git? ›

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.

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.

How do I reset my local master to Origin? ›

Use git fetch origin to retrieve the latest updates from the remote. Use git checkout master to switch to the master branch. Use git reset --hard origin/master to reset the local master branch to match the one on the remote.

How do you squash commits? ›

In case you are using the Tower Git client, using Interactive Rebase to squash some commits is very simple: just select the commits you want to combine, right-click any of them, and select the "Squash Revisions..." option from the contextual menu.

How do I fetch a remote branch? ›

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands. If you approve the changes a remote branch contains, you can merge it into a local branch with a normal git merge .

What is the difference between a force push and a push? ›

Force: Push and Pull

A force that changes the direction of an object towards you, would be a pull. On the other hand, if it moves away, it is a push. Sometimes, force is simply defined as a push or pull upon an object resulting from the object's interaction with another object.

What happens if I force push a branch? ›

Force push with lease

If someone else contributes to your branch and pushes up their changes to the remote—and you force push over it—you will overwrite their changes. To prevent this scenario, you can use the --force-with-lease option.

Can I commit in detached head? ›

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.

How to check all committed files in git? ›

Find what file changed in a commit

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

What is one disadvantage to entering the detached head state? ›

The problem with a detached HEAD

You are automatically on the newest commit of the chosen branch. When you instead choose to check out a commit hash, Git won't do this for you. The consequence is that when you make changes and commit them, these changes do NOT belong to any branch.

What is head detached at tag? ›

When you checkout the tag, git tells you that you are in "detached HEAD" state. Do not worry, all it means is that you need to create a new branch if you want to retain any changes you make after checking out the tag.

What is git reset used for? ›

Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

What is a detached head in Travis CI? ›

Detached Head (Monorepo)

Some plugins might use tools that require you to be on a branch. The default setup for travis leaves you in a "Detached Head" state, meaning the git HEAD pointer is not on a branch.

Why does my head feel detached? ›

Passing feelings of depersonalization or derealization are common and aren't necessarily a cause for concern. But ongoing or severe feelings of detachment and distortion of your surroundings can be a sign of depersonalization-derealization disorder or another physical or mental health disorder.

What does pressure head feel like? ›

Signs and symptoms of a tension-type headache include: Dull, aching head pain. Sensation of tightness or pressure across the forehead or on the sides and back of the head. Tenderness in the scalp, neck and shoulder muscles.

How do I checkout a tag? ›

In order to checkout a Git tag, use the “git checkout” command and specify the tagname as well as the branch to be checked out. Note that you will have to make sure that you have the latest tag list from your remote repository.

How to reset the reset git? ›

git reset --soft : Known as a soft reset, this updates the current branch tip to the specified commit and makes no other changes. git reset --hard : Known as a hard reset, this updates the current branch tip to the specified commit, unstages any changes, and also deletes any changes from the working directory.

How do I completely reset a git repository? ›

How to reset a Git branch to a remote repository
  1. Save the state of your current branch in another branch, named my-backup ,in case something goes wrong: git commit -a -m "Backup." git branch my-backup.
  2. Fetch the remote branch and set your branch to match it: git fetch origin. git reset --hard origin/master.

How do I reset my entire 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 does Travis CI work? ›

CI Builds and Automation: Building, Testing, Deploying #

When you run a build, Travis CI clones your GitHub repository into a brand-new virtual environment, and carries out a series of tasks to build and test your code. If one or more of those tasks fail, the build is considered broken.

How do I remove Travis CI from GitHub? ›

Uncheck Travis CI status checks from your repository Settings > Branches > Branch protection rules:
  1. Travis CI — Branch.
  2. Travis CI — Pull Request.

Can I use Travis CI for free? ›

Travis CI free Trial plan will provide you with 10,000 build credits to try it out for public and private repositories builds and unlimited number of users with no charge. Once you use up all credits or they expire, simply do not select any other plan.


1. Git Errors Detached Head
(Dave Carrigg)
2. you are in 'detached head' state in Git
(Sagar S)
3. What is detached HEAD state and how do I get out of it in Git || Github || detached HEAD in Git
(KK JavaTutorials)
4. [Version Control] detached HEAD state in git | How to fix a detached HEAD & a brief HEAD tutorial
5. 35. What is a "Detached HEAD" in Git | What is detached HEAD state | Git tutorial for beginner
(Teach YourSelf Code)
6. How do I fix a Git detached head?
(Peter Schneider)


Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated: 11/10/2023

Views: 5849

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.