Data report"State of code review 2024" is now liveRead the full report

How to resolve the git error message "you are not currently on a branch"

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The message "you are not currently on a branch" indicates that your HEAD is detached. In Git, the HEAD is a pointer that tracks the current branch or commit your repository is on. When the HEAD is detached, it points directly to a commit rather than a branch. This situation can happen in several scenarios, such as:

  • Checking out a specific commit: When you checkout a commit instead of a branch, Git detaches the HEAD to allow you to view the repository at that state without affecting the current branch.
  • Pulling changes after a shallow clone: If you've done a shallow clone (a limited history of the repository) and try to pull changes, Git may not be able to find the current branch history, leading to a detached HEAD.
Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2
  1. Identify current HEAD position

    Start by checking your current HEAD position to confirm it's detached:

    Terminal
    git status

    If detached, the output will say something like "HEAD detached at <commit>."

  2. Reattach HEAD to a branch

    If you know which branch you should be on, you can simply checkout to that branch:

    Terminal
    git checkout <branch-name>

    If you are unsure which branch to checkout, list all branches with:

    Terminal
    git branch

    and find the most appropriate one for your work.

  3. Create a new branch (if necessary)

    If there’s no existing branch you want to attach to, or you want to keep the changes made while HEAD was detached, create a new branch:

    Terminal
    git checkout -b new-branch-name

    This command will create and switch to a new branch starting from the current commit.

  4. Perform git pull on the correct branch

    Once you are on a branch, you can safely pull changes from the remote repository:

    Terminal
    git pull <remote> <branch>

    Make sure to replace <remote> and <branch> with the appropriate remote repository name and branch name, respectively.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
  • Avoid direct checkouts to commits unless necessary for specific tasks like reviewing older states of the project.
  • Regularly update local branches with changes from their remote counterparts to minimize conflicts and errors.

For further reading see this guide on how to resolve the Git detached head state message.

On this page
Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2