World map

 

BM: Baca dalam Bahasa Melayu

World 11: Rescue Center

I'm in detached HEAD

You will learn: how to leave a detached HEAD without losing commits.

The problem

git status says: HEAD detached from a hash. Nothing to commit, working tree clean. Terminal output: $ git status HEAD detached from a8feb63 nothing to commit, working tree clean

Git says HEAD is detached. Sounds scary! It only means HEAD sits on a commit, not on a branch. This happens to everyone.

Watch

HEAD jumps back to main. The commit Try a new title turns into a dashed dot: no branch holds it.
6d3a9b1a8feb63270a76e982797e

You made a commit while detached. Leave now, and it gets left behind. No branch holds it.

Try it

You want to keep that commit. What do you do first?

Pick one answer.

Try it

Keep your commit. Make a branch called new-title, right here.

6d3a9b1a8feb63270a76e982797e

Predict

New story: you only looked around. No new commits. How do you get home?

Pick one answer.

You did it!

You can now leave a detached HEAD without losing commits.

Go deeper (optional)

Already left, and the commit is gone? The reflog remembers. Right after leaving, git branch new-title HEAD@{1} brings it back. Git also prints the commit’s hash when you leave. git branch new-title plus that hash works too.