How do I "move" my commits from "no branch" to an actual branch?

Another solution, which does not involve creating a temp branch, is described here. You just merge with you last commit rather than a temp branch.

$ git checkout master
$ git merge d2bdb98

If you don't know what commit you are on you can find it with git log. I don't know if this solution is different than "cherry-picking" but it had the expected results for me.


You can view all your commits using git reflog

So you can simply got to another branch, and do git cherry-pick <commit-hash> for the required commits.

But I'd prefer the branch way as spatz mentioned.


Note: you have also

  • a complete explanation of detached HEAD in "detached HEAD explained"
  • a nice illustration of its effect in "Git Is Your Friend not a Foe Vol. 3: Refs and Index", when doing a checkout of a commit instead of a branch.

enter image description here

In both case, doing a tmp branch and merging it back to the actual branch is the solution.


You are currently in a detached HEAD state. To resolve that, all you need to do is create a new branch with git branch <branchname> or git checkout -b <branchname>. That will leave you with a local branch you can play with, and even delete when you're done with it.

Branches in git are just pointers to commits, so if you create a new branch where you are the new branch will point to your current commit, and then you can merge it or whatnot.

Your "mistake" need not be erased, you simply created new commits on top of previous ones. You did not modify history or anything like that.

EDIT: In response to your comment, what you need to do is:

git branch temp
git checkout master # or any other branch
git merge temp
git branch -d temp