Getting back code from detached commit with no branch

最后都变了- 提交于 2019-12-10 20:21:52

问题


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?


回答1:


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.




回答2:


git checkout -b newbranch

Creating a new branch with the recent changes committed to that branch (your no branch) and also checkout to that branch (newbranch)

git push origin master

Pushes the changes to master branch



来源:https://stackoverflow.com/questions/19469839/getting-back-code-from-detached-commit-with-no-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!