Detached Heads
Learn how you can switch between different commits either in the same branch or across branches.
We'll cover the following
Sometimes when using Git you might have seen something like this:
git status
HEAD detached at 76d43b6
Detaching#
The HEAD
pointer can be moved to an arbitrary point. In fact, git checkout
does exactly this. You can specify a reference (like master
or newfeature
) or a specific commit ID.
The next set of commands will check out the repository at a particular commit:
1 mkdir lgthw_git_branch_2
2 cd lgthw_git_branch_2
3 git clone https://github.com/ianmiell/shutit.git
4 cd shutit
5 git log
After running the above commands, you will see the log output in the terminal. Hit q
to exit the log output, and then run the following command to point the HEAD
pointer to a specific commit:
6 git checkout e36355ed00ac3af009d7113a9dd281c269a79afd
Read the text carefully and slowly. Make sure you’ve tried to understand what it all means.
Back to our best friend git status
:
7 git status
The detached HEAD
part of this gives people the most concern. A detached HEAD
sounds like a bad thing and can be bewildering upon seeing it.
It is quite simple though. It just means that your Git repository’s HEAD
pointer (which is where Git currently thinks it is) is not pointed at a branch at the moment. Instead, it’s pointed at a commit ID, i.e., the HEAD
is detached from a branch. There might be a branch pointed at that commit (git log --decorate
will tell you this), but your HEAD
is not pointed at it.
Think ahead#
Think about this and ponder the advice you’d give to someone in a detached HEAD
state to reorient themselves, given what you’ve been taught so far about branches and git log
. Answering questions like this is the best way to understand Git.