【问题标题】:Recover lost data GIT detached head恢复丢失的数据 GIT 分离头
【发布时间】:2022-01-14 02:37:38
【问题描述】:

我不太擅长 GIT(我并没有真正理解这个 HEAD 和“掌握”的东西)而且我刚刚丢失了我的代码。谁能帮我找回它?

我的步骤是这样的:

  1. 我添加了一些代码,我想提交它
  2. 弹出一个窗口,显示“警告:HEAD 已分离”之类的消息,但我点击了“仍然提交”
  3. 我切换到以前的提交,因为我想在旧代码中寻找一些东西

现在,我不能回到第一步。我尝试了一些在 stackoverflow 上找到的东西,但我无法取回我的原始代码(从步骤 1 开始)。 它是永远丢失了还是有办法找回它?

我在 GIT 中使用的是 PHPStorm 图形界面,但我似乎可以在控制台中看到我的所有步骤,所以我将把它粘贴到下面,这样你就可以看到我的步骤到底是什么:

08:19:51.448: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/common/VuexStoreCategories.js
08:36:06.443: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false reset -- components/vue/summary/SummarySum.vue.php
08:36:06.585: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false rm --ignore-unmatch --cached -r -- <list_of_some_files_with_my_code>
08:36:06.694: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- <list_of_some_files_with_my_code>

<warnings about replacing LF with CRLF>

08:36:08.397: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- <list_of_some_files_with_my_code>

<warnings about replacing LF with CRLF>

08:36:08.835: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Przemek\AppData\Local\Temp\git-commit-msg-.txt --
[detached HEAD 030b7b5] w połowie rozbijania vuex summary na moduły
 286 files changed, 11322 insertions(+), 5531 deletions(-)
 create mode 100644 AJAX/logout.php
 <list of info about files changes like "create", "rewrite", "rename" ... >

08:36:50.494: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout a28074265e45de506ab69098cfa97ceb4908e957 --
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
  030b7b5 w połowie rozbijania vuex summary na moduły
If you want to keep it by creating a new branch, this may be a good time
to do so with:
 git branch <new-branch-name> 030b7b5
HEAD is now at a280742 Update 08.09.2021
08:38:24.842: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
Previous HEAD position was a280742 Update 08.09.2021
Switched to branch 'master'
08:40:00.813: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout 41ef9415a73ff920c89d1caccde2768521da9a34 --
Note: switching to '41ef9415a73ff920c89d1caccde2768521da9a34'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
  git switch -c <new-branch-name>
Or undo this operation with:
  git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 41ef941 w połowie przenoszenia summary do vue
09:05:53.832: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false cherry-pick 41ef9415a73ff920c89d1caccde2768521da9a34
Auto-merging classes/TestClass1.php
Auto-merging classes/TestClass2.php
On branch alt-history
You are currently cherry-picking commit 41ef941.
Untracked files:
The previous cherry-pick is now empty, possibly due to conflict resolution.
    AJAX/hello world.xlsx
If you wish to commit it anyway, use:
    AJAX/temp.php
    TO_LOAD/
    git commit --allow-empty
    exporters/
Otherwise, please use 'git cherry-pick --skip'
    <list of graphic files excluded from git>
nothing added to commit but untracked files present

【问题讨论】:

  • 查看错误信息,即在“Warning”之后。 Git 会准确告诉您重新创建分支以恢复丢失的提交的操作过程 (git branch &lt;new-branch-name&gt; 030b7b5)
  • @RomainValeri 好的,我发出了如下命令:'git brach odzyskana 030b7b5'。我仍然看不到我的文件。我现在该怎么办?我应该以某种方式切换到这个新分支吗?
  • 好的,我明白了 :) 我使用“git branch odzyskana”切换到新的分支,然后我必须右键单击我的 PHPStorms 文件树并单击“从磁盘重新加载文件” .我把我所有的文件都拿回来了。非常感谢。你救了我的命:D

标签: git git-detached-head


【解决方案1】:

问题解决了。 正如 Romain Valeri 在评论中提到的,我必须执行以下步骤:

  1. "git branch [new-branch-name] 030b7b5" - 用我丢失的文件创建一个新的分支(这个 030b7b5 来自我的日志)
  2. “git branch [new-branch-name]”切换到这个新分支
  3. 在 PHPStorm 中点击“从磁盘重新加载文件”

再次感谢 Romain Valeri 解决问题 ;)

【讨论】:

  • 不客气。感谢您的反馈;-)
猜你喜欢
  • 2019-10-07
  • 2016-06-15
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2019-06-15
相关资源
最近更新 更多