Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging a Detached Head?

Tags:

git

git-merge

So I kind of messed up royally and created a detached head because I wanted to go back to a previous version of my code. Problem is: now I can't push anything because my main branch is a detached head. If I try to merge the branches then it gets rid of the head and anything on it. How do I get the commits on my detached head back onto the master branch? I heard about this code: git merge HEAD@{1} but I was scared to try it without confirmation because I was afraid of what would happen. Anyone know how to do this?

like image 296
user2253215 Avatar asked Dec 19 '22 18:12

user2253215


1 Answers

because my main branch is a detached head

No, that’s not correct. A detached HEAD is when you have checked out a commit that is not a branch. So by definition, you are not on a branch if you have a detached HEAD.

I would suggest you to create a branch from your current (detached) HEAD, so you don’t lose any information but can move around freely again:

git checkout -b newbranch

After that, you will be on a non-detached HEAD again, on newbranch, and you should be able to check out other branches and merge newbranch in some other branch if necesary.

like image 63
poke Avatar answered Jan 01 '23 20:01

poke