Skip to content

How to Fix a Detached HEAD on a Git Repository

Summary

Occasionally, when making changes to a branch of a git repo, the HEAD (last commit on a branch) may become detached from the branch. This most commonly happens when a single commit is checked out from the commit history. This is problematic because if it isn't resolved, any additional work will need to be stashed until the HEAD can be reattached to a particular branch.

How to Identify a Detached HEAD

There are a handful of ways to identify a detached HEAD. One of which is in the Source List of a code editor. It will show the commit that's been checked out instead of the name of the branch (ex. Master branch). Another is trying to run git pull. It will likely provide the following error:

You are not currently on a branch. Please specify which branch you want to merge with.

How to Reattach the HEAD to a Git Branch

The HEAD of a git branch can be reattached in a number of ways, but the following is the easiest method I've used so far...

  • Check all files and revert all changes.
git status
  • Switch branch to Master
git checkout master
  • Update repository
git pull

References