简体   繁体   English

如何从'分离头状态'中恢复提交?

[英]How to recover commit from 'detached head state'?

I checked out another branch with updates then made a few changes, switched back to the main git and now the changes disappeared! 我检查了另一个更新的分支,然后进行了一些更改,切换回主git,现在更改消失了! Can I get them back? 我能把它们还给他们吗? the terminal was basically: 终端基本上是:

$ git commit
[detached HEAD 7c09e17] Fixed some stuff
  files changed, insertions(+), deletions(-)
$ git push master
fatal: 'master' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git checkout master
Previous HEAD position was 7c09e17... Fixed some stuff
Switched to branch 'master'
$ git merge theother/directory

Assuming you're still on master: 假设你还在掌握:

git merge 7c09e17

should be enough. 应该够了。 git is usually good about telling you the commit IDs, if you watch the terminal. 如果你看终端, git通常很好地告诉你提交ID。

I had a similar problem. 我遇到了类似的问题。 I found git reflog to be a life-saver. 我发现git reflog可以节省生命。 In case it helps illustrate it use, here's the output: 如果它有助于说明它的使用,这里是输出:

e3191c5 HEAD@{0}: checkout: moving from ec31ccf0735240d0cdc5a44fd443039c3caa43f0 to master
ec31ccf HEAD@{1}: commit: Added code and data for simulation.
781b9ee HEAD@{2}: checkout: moving from 3bd804e635b913840c71b7f8a33665460580d45f to 781b
3bd804e HEAD@{3}: checkout: moving from master to 3bd804

My situation was a bit different in that I had made a commit while in a detached HEAD state starting from a very old commit. 我的情况有点不同,因为我从一个非常旧的提交开始在一个独立的HEAD状态下进行了提交。

If I simply wanted to merge ec31ccf0735240d0cdc5a44fd443039c3caa43f0 (aka ec31ccf , which is where I had been) into master , I think git merge ec31ccf or git rebase ec31ccf might have worked. 如果我只是想将ec31ccf0735240d0cdc5a44fd443039c3caa43f0 (又名ec31ccf ,这就是我曾经去过的地方)合并master ,我认为git merge ec31ccfgit rebase ec31ccf可能有效。 But this would be mostly merging ancient history in my case (with merge conflicts, etc.). 但这主要是在我的案例中合并古代历史(合并冲突等)。

Instead, I just wanted to recover what I'd done on ec31ccf , and git cherry-pick ec31ccf worked nicely. 相反,我只是想恢复我在ec31ccf所做的ec31ccf ,而git cherry-pick ec31ccf工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM