Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting back code from detached commit with no branch

I have a commit that is not in a branch and it contains all my latest work.

I need to add it to the master branch. How can I do this?

git status shows this:

* (detached from) b225b49
  master
  saved-work

Will I lose it if I git checkout master?

like image 373
jimbob Avatar asked Jul 23 '26 19:07

jimbob


1 Answers

You're in what git calls "detached HEAD" state.

There's a super-easy way to recover:

git checkout -b newbranch

creates the new branch newbranch and puts you on it, at the current commit (i.e., nothing changes except that you're now "on" a branch named newbranch). You can then do whatever you like on the new branch (add more commits, whatever) and then merge it into some other branch, or whatever else you need to do.

like image 168
torek Avatar answered Jul 25 '26 10:07

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!