How to make a commit in a detached HEAD state – 098

And one more quote from the same stack overflow answer, “it will still allow you to make commits (which will update HEAD), so the above short definition is still true if you think of a detached HEAD as a temporary branch without a name.”

What? We can still make commits? in a detached HEAD state?

Let’s be brave and give it a try in the visualization tool.

If we do a ‘git commit’ in the detached HEAD state, the visualization tools shows a commit being made to the side.

visualizing commit made in detached HEAD state

P.S. Have I proclaimed my love of this git visualization tool yet today?

So what on earth now? we have this thing floating out in nowhere. Well, we know it is a nameless branch… and we know we can merge branches… let’s try to merge it!

First, we’ll checkout master.

back to non-detached head but commit still floating out there

Then we’ll do the merge and verify the git log.

merged in floating git commit

and thus conclude my mathematical proof I truly have no idea how people are able to keep track of these one-off commits or whatever is going on in git without visualization tools. QED.

2 thoughts on “How to make a commit in a detached HEAD state – 098

  1. The way to keep track of these, unless you plan on doing exactly what you did in the post, merging them immediately while you still have/know their id’s, is to create a branch.

    When you’re in detached HEAD state, simply create a branch:

    git branch LIMBO

    And now you have a branch attached to the commit. If you now checkout another branch or commit, the “LIMBO” branch will still point to the commit you made and keep it alive.

    I tend to avoid getting into a detached HEAD state if I can by simply creating the branch immediately when checking out the commit, this can be done with:

    git checkout SHA -b LIMBO

    Later you can simply delete the branch if your history spelunking didn’t bear fruits.

    Like

    1. Nice! I didn’t even think of creating a branch while in this “limbo” state. I also had no idea `git checkout SHA -b LIMBO` would work! Thank you!! I have some exploring to do and new tips to write!

      P.S. how might a person get into a detached HEAD state where they have to create a commit? Best I can come up with is fixing a bug that has to be deployed for a particular build.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s