What to do with commit made in a detached head

Create a branch where you are, then switch to master and merge it:

git branch my-temporary-work
git checkout master
git merge my-temporary-work

This is what I did:

Basically, think of the detached HEAD as a new branch, without name. You can commit into this branch just like any other branch. Once you are done committing, you want to push it to the remote.

So the first thing you need to do is give this detached HEAD a name. You can easily do it like, while being on this detached HEAD:

git checkout -b some-new-branch

Now you can push it to remote like any other branch.

In my case, I also wanted to fast-forward this branch to master along with the commits I made in the detached HEAD (now some-new-branch). All I did was:

git checkout master

git pull # to make sure my local copy of master is up to date

git checkout some-new-branch

git merge master # this added the current state of master to my changes

Of course, I merged it later to master.

That's about it.


You could do something like this.

# Create temporary branch for your detached head
git branch tmp

# Go to master
git checkout master

# Merge in commits from previously detached head
git merge tmp

# Delete temporary branch
git branch -d tmp

Even simpler would be

git checkout master
git merge HEAD@{1}

but this has the slight danger that if you do make a mistake it can be a little harder to recover the commits made on the detached head.