March 27th, 2018
I’ve recently seen multiple questions online from people having problems committing to git because the git repository had a detached head. This article answers the most common questions about detached heads.
A detached head in a git repository occurs when the repository has no current branch.
A detached head isn’t a problem until you commit some changes. If you commit changes with a detached head, the new commit exists, but it will not appear in any branch’s commit history. The only way to access a commit from a detached head is to memorize the commit number. Git commit numbers are long, random hexadecimal numbers, making them difficult to remember.
Checking out a specific commit is the most common way to detach the head from a git repository.
Check out a local branch. The branch you check out will become the current branch, the attached head of the repository.
To checkout a local branch in Xcode, open the source control navigator by choosing View > Navigators > Show Source Control Navigator.
Notice the long hexadecimal number number next to the SpriteKitGitDemo folder in the screenshot. That number is the commit number. The fact that there’s a commit number instead of a branch name next to the folder means the repository has a detached head.
The local branches are inside the Branches folder. Click the disclosure triangle to show them.
Select a branch, right-click, and choose Checkout to check out the branch.