Sometimes applications give us very strange error messages and we have to rack our brains to understand exactly what the problem is and how to fix it. As for new developers, using Git as a version control software sometimes can be daunting at times, because of the weird error messages it spits out.
In this article, we walk around "Cannot publish, unborn HEAD" – a common Git error message which often cause new developers headache just to make it go away. We will also explain the concepts around it and how to avoid it in the future.
What is HEAD in Git?
HEAD is a pointer whose job it is to reference a specific commit in the repository. Usually it points to the last commit you made.
You can think of the HEAD as the reference to the "current branch". You move, HEAD moves along with you.
To see what "current branch" you’re on, you can use the following command :
cat .git/HEAD ref: refs/heads/master
HEAD can also points to the last commit that was checked out into the working space. This behaviour is called detached HEAD.
Every time you use
git checkout [commit_id], you’re essentially moving HEAD to the commit with ID
"Cannot publish, unborn HEAD", what does it means?
So now that you know that HEAD is the "current branch", you can ask : What about an empty Git repository? What does HEAD point to after I’ve just created a repository and did not commit anything to it? The answer to the questions above is exactly the reason behind the error.
Before you commit the first time in a new repository, the HEAD doesn’t exist. "Cannot publish, unborn HEAD" basically means you are attempting to publish a branch that has no commits.
Fix "Cannot publish, unborn HEAD"
The solution is very simple. If you’re using git command line interface, you can simply do a
push, the error message will immediately go away.
If you create a new repository from Github’s web-based interface, please follow the instruction provided to properly initialized and do the first commit.
Alternatively, you can automatically add a README.md file by ticking in Add a README file when you create a new repository.
If you don’t want any README in your repository, you can add a summary and description, then commit them to the branch.
git commit -m "Title" -m "Description ..........";
Github Desktop simplify this process, you only have to input the Summary and Description then press Commit to master.
We hope that the article helps you understand why the error happens in the first place, and quickly fix it. You may be interested in our other articles about Git/Github as well :
- Git submodule tutorials
- Clone a git repository and add a submodule
- .gitmodules file
- Delete a git submodule
- How to configure a proxy to work with git