46
dave@dave-dev:/media/dev/belgravia$ git branch
* (no branch)
  master

I'm not sure how this happened, but is there a way I can merge no-branch into the master. I'm not sure how I can merge two branches when one of them is not a branch. The commits in no-branch seem to be loose. I'm afraid that checkout master will cause data loss.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Benbob
  • 13,876
  • 18
  • 79
  • 114
  • In my case, that just happened after a conflict during `git rebase`. Just do `git rebase --continue` and it bring you back to the branch you were on before. – Frank Aug 14 '12 at 21:32
  • also related: http://stackoverflow.com/questions/366093/git-commit-against-tag-with-no-branch – rogerdpack Dec 20 '12 at 22:26

3 Answers3

70

Use git show to get the SHA1 commit ID of the current HEAD. With that information, you can't lose those commits.

Then, switch to master and:

git merge abc123

where abc123 is the SHA1 from the first step.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 5
    What I ended up doing was `branch temp`, `checkout master`, `merge temp`. That moved no branch into an actual branch, then I could merge it. Your solution is more simple however. – Benbob Jan 13 '11 at 02:51
  • 1
    @Keyo: It didn't move "no branch" into a branch. It created a branch at your current HEAD (currently checked-out commit). When git branch said "no branch", it meant just that: you had no branch currently checked out. This is also known as detached HEAD. – Cascabel Jan 13 '11 at 03:44
  • Instead of git show, you can also use something like log --graph -20 --pretty=oneline - just to get the SHAs, but for the last 20 commits. Easier to check where you blundered, I think. – Zlatko Nov 25 '12 at 12:29
4

Maybe you can commit it on current branch(no-branch)

Then you have to do:

git reflog

After that you can get the id of this commit like 1d84d08

do:

git checkout master 
git merge 1d84d08
Alexandre Marcondes
  • 5,859
  • 2
  • 25
  • 31
Justin_lu
  • 41
  • 2
3

The reason you have (no branch) is you have done:

git checkout REMOTE_BRANCH_NAME
  • In order for you to work locally on that branch you have to do: git checkout -b local_branch_new_name
  • now do a: git branch - a
  • you will see:

local_branch_new_name

  master
  • From here you can merge a branch into master the usual way. switch to master and do:

    git merge local_branch_new_name

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
venkat
  • 33
  • 2