1

我不太擅长 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
4

1 回答 1

1

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

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

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

于 2021-12-09T09:12:39.220 回答