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

38,423

Solution 1

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

Solution 2

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.

Solution 3

Note: you have also

enter image description here

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

Solution 4

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.

Solution 5

I just had a situation where I had a master branch I wanted to commit to, but accidentally committed to a detached HEAD (note, the commit hash is not important here, but just to illustrate the situation):

$ git branch
* (detached from 29d2cfb)
  master

Here was my quick fix:

$ git branch -f master HEAD
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Piece of cake.

Note that this trick only works if master originally pointed to the same commit you made your mistaken commit on top of. If that's not the case, you'll have to rebase (or cherry-pick, or merge...).

Share:
38,423
Letharion
Author by

Letharion

Updated on July 27, 2022

Comments

  • Letharion
    Letharion almost 2 years

    I made a mistake, and started making commits "to the last tag", which lands my commits in "no branch". They should have been applied at the head of an already existing branch. I have not pushed my changes yet. I've found a few other question for when commits are in the wrong branch, but now I have no branch, so I don't know how to handle this.

    Preferably, I would like to erase my mistake entirely, and "move" my changes to the end of the right branch. If I must leave my mistake in the history, I need to merge them in atleast.

  • Letharion
    Letharion over 12 years
    If I understand you correctly, that will create a new branch, but I want to apply all the commits to an already existing branch. I've updated my question to make that more clear.
  • drrlvn
    drrlvn over 12 years
    @Letharion, then all you need to do after creating a new branch, is to merge it to the branch where you want these changes, and then you can delete the new temp branch.
  • CharlesB
    CharlesB over 12 years
    In this case you'll want to rebase on main branch instead of merge
  • drrlvn
    drrlvn over 12 years
    @CharlesB, The default behavior of git merge is to fast-forward if possible, which is the same as rebase.
  • CharlesB
    CharlesB over 12 years
    @spatz: Sure but I assume that the merge won't be fast-forward in this case, because commits were made starting from an ancient tag.
  • Cascabel
    Cascabel over 12 years
    @Letharion: Then after creating the branch, run git rebase master temp to transplant the commits onto master, before checking out master and merging into it. (The merge will be a fast-forward then.)
  • Letharion
    Letharion over 12 years
    I followed the instructions spatz gave, and it worked well. I'm not sure how the rebase suggestion plays in, but I'll refer to that if I happen to do the same again.
  • Marcello de Sales
    Marcello de Sales about 11 years
    Just went through this scenario today with a co-worker. He had checked out another branch, but we could see the commit hash in the shell :)
  • wangzhengyi
    wangzhengyi about 8 years
    Thank you, this is very useful.
  • jan-glx
    jan-glx over 7 years
    It is different and merging is want you want in this case. If you made more than one commit in detached HEAD state, cherry-picking would only recover the changes of the latest.
  • jan-glx
    jan-glx over 7 years
    git reflog lists commits of other (and no) branches too and is more useful in this scenario
  • Stefan Monov
    Stefan Monov about 7 years
    @spatz: The behavior of git branch temp that I know of is to create a branch without switching to it. Does it have a special meaning of "also assign all no-branch-owned commits to this new branch"?
  • pabrams
    pabrams over 5 years
    This doesn't work. git branch master yields "fatal: not a valid object name: 'master'"