How to Push a detached git HEAD
Today’s article covers a quick method of committing to remote branches while in a detached HEAD state.
Requirements
If you would like to follow along these are the things you need:
- a forked github repository, you can fork any of my repositories
- a local copy of the repository, for example, my emacs configuration:
$ git clone https://github.com/a-leung/rails_api
detached HEAD ???
It is a state when the current git branch does not have an explicit reference to a branch.
To get into a detached state, just checkout the equivalent remote branch like so:
In this state, one can work as if the repository was tracked, but it’s not since this message will always appear.
Up to now, I always thought I had to make a full branch before being able to push changes to the remote.
Surprise, Surprise
I learned that the current detached HEAD can be pushed by the following command:
In one case, it can be:
If one wanted to write new commits directly to the remote master branch without having local changes!
This basically means HEAD
is the current branch and is a reference
that can be used the same as a branch. Neat!
Normally, I would recommend against such practices, but there may be circumstances that having this knowledge would be useful.