I ended up with a detached head today, the same problem as described in: git push says everything up-to-date even though I have local changes
As far as I know I didn't do anything out of the ordinary, just commits and pushes from my local repo.
So how did I end up with a detached HEAD
?
I reproduced this just now by accident:
lists the remote branches
I want to checkout one locally, so I cut paste:
Presto! Detached HEAD state
Solution #1:
Do not include
origin/
at the front of my branch spec when checking it out:Solution #2:
Add
-b
parameter which creates a local branch from the remotegit checkout -b origin/Feature/f1234
orgit checkout -b Feature/f1234
it will fall back to origin automaticallyWhen you checkout to a commit
git checkout <commit-hash>
or to a remote branch your HEAD will get detached and try to create a new commit on it.Commits that are not reachable by any branch or tag will be garbage collected and removed from the repository after 30 days.
Another way to solve this is by creating a new branch for the newly created commit and checkout to it.
git checkout -b <branch-name> <commit-hash>
This article illustrates how you can get to detached HEAD state.