Black Friday sale: 30% off Pro and Unlimited, until 22 December

How to save commits from a detached HEAD

Spot a detached HEAD, name its current tip with a branch, and check the commits are safe before you switch away.

You checked out a tag or a commit to look around, then fixed something while you were there. Those commits belong to no branch. If you switch away now, Git lets you go, and the only way back is a commit ID you may not have kept.

Name the tip first. It takes one command.

Spot a detached HEAD

Run this in the repository. In this example it’s ~/code/acme/web, with v1.4 checked out.

git status
HEAD detached from v1.4
nothing to commit, working tree clean

“Detached from” means you’ve committed since the checkout. A fresh checkout says “detached at” instead. To check from a script, ask for the current branch.

git branch --show-current

On a branch this prints its name. Here it prints nothing. git symbolic-ref -q HEAD does the same job and exits with 1 when HEAD is detached.

git log --oneline -3
e95ff57 Add subtotal test
1a19664 Round cart subtotal
0639671 Add cart page

The first two lines are yours. The third is v1.4, where you started.

Name the tip with a branch

Commit anything you want to keep first. Then create a branch at your current commit and switch to it.

git switch -c rescue/cart-subtotal
Switched to a new branch 'rescue/cart-subtotal'

Without a start point, git switch -c uses the commit you’re on. Pick a name you’ll recognise next week. Uncommitted changes come along to the new branch.

Check the commits before you switch away

Show what the branch holds that main doesn’t.

git log --oneline main..rescue/cart-subtotal
e95ff57 Add subtotal test
1a19664 Round cart subtotal

Both commits are there. Then confirm the branch is where you are and nothing is waiting in the working tree.

git status --short --branch
## rescue/cart-subtotal

The branch name is on the first line, and there are no file lines below it. Now you can leave.

git switch main

The branch stays, with both commits on it. If you switch away from a detached HEAD before naming it, Git prints a warning like this one. Run the git branch line it gives you straight away, with your own commit ID.

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  8007f2d Round cart subtotal

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 8007f2d

Push the branch when it’s ready

A new branch has no upstream yet, so the first push needs -u. See how to fix “The current branch has no upstream branch”.

If the detached HEAD was in a linked worktree, the branch you made is shared with the other worktrees. Remove the worktree only after the branch holds your commits. How to clean up old Git worktrees shows how to check before removing one.

See detached checkouts across your repositories

push& (Pushand) lists your repositories and worktrees in the menu, detached checkouts included, so you can spot one before you leave. See how to read the menu.

More Git how-tos