Git“自动压缩”似乎已从分支中删除了所有提交记录

4

在我提交之前,我使用 git ..(我在子目录中)进行了所有更改的暂存,并运行了 git status 以查看已暂存的更改。此时,Git仅暂存了更改的文件,就像预期的一样。

在命令行中,我运行 git commit 并写上一条消息,得到以下响应:

Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
[SelectingDate 910641c4] Switching from many visibility animators to one translate animated view. Cuts down time to update list significantly.
 7 files changed, 43 insertions(+), 15 deletions(-)
 rename mobile/features/itemLists/CursorItemsCoordinator/{AnimatedVisibilityCursor.native.js => AnimatedTranslatingCursor.native.js} (52%)

虽然我不太习惯看到“自动打包”消息,但我已经找到了其他文章可以帮助我摆脱它。然而,根据他的Cl响应,更改似乎已经提交。

接着我立即运行git status,令我惊讶的是,这是响应:

On branch SelectingDate

No commits yet

然后它列出了我仓库中所有文件都已经添加至缓存区。

我的仓库中的文件似乎已更新到它们当前的版本,这很好。有人知道可能是什么导致删除了我当前分支上的所有提交/如何将它们恢复(如果可能的话)吗?

以下是完整的命令行交互:

➜  mobile git:(SelectingDate) ✗ gs
On branch SelectingDate
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    features/itemLists/CursorItemsCoordinator/AnimatedVisibilityCursor.native.js
    modified:   features/itemLists/CursorItemsCoordinator/CursorItemsCoordinator.native.js
    modified:   features/itemLists/CursorItemsCoordinator/interpolaters.native.js
    modified:   features/itemLists/MainListView/MainItemsList.native.js
    modified:   helpers/animatedIndexHelper.native.js
    modified:   ../shared/itemLists/listTypes/plan/PlanItemsContainer.shared.js
    modified:   ../web/src/__test__/native/interpolaters.test.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    features/itemLists/CursorItemsCoordinator/AnimatedTranslatingCursor.native.js

no changes added to commit (use "git add" and/or "git commit -a")
➜  mobile git:(SelectingDate) ✗ git add ..
➜  mobile git:(SelectingDate) ✗ gs
On branch SelectingDate
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    renamed:    features/itemLists/CursorItemsCoordinator/AnimatedVisibilityCursor.native.js -> features/itemLists/CursorItemsCoordinator/AnimatedTranslatingCursor.native.js
    modified:   features/itemLists/CursorItemsCoordinator/CursorItemsCoordinator.native.js
    modified:   features/itemLists/CursorItemsCoordinator/interpolaters.native.js
    modified:   features/itemLists/MainListView/MainItemsList.native.js
    modified:   helpers/animatedIndexHelper.native.js
    modified:   ../shared/itemLists/listTypes/plan/PlanItemsContainer.shared.js
    modified:   ../web/src/__test__/native/interpolaters.test.js

➜  mobile git:(SelectingDate) ✗ gc "Switching from many visibility animators to one translate animated view. Cuts down time to update list significantly."
Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
[SelectingDate 910641c4] Switching from many visibility animators to one translate animated view. Cuts down time to update list significantly.
 7 files changed, 43 insertions(+), 15 deletions(-)
 rename mobile/features/itemLists/CursorItemsCoordinator/{AnimatedVisibilityCursor.native.js => AnimatedTranslatingCursor.native.js} (52%)
➜  mobile git:(SelectingDate) ✗ gs
On branch SelectingDate

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   ../.gitignore
    new file:   ../.prettierignore
    new file:   ../.yarn/install-state.gz
    new file:   ../README.md
    new file:   .eslintrc.json
    new file:   .expo-shared/assets.json
    new file:   .prettierrc.json
    new file:   App.native.js
    new file:   app.json
    new file:   assets/adaptive-icon.png
    new file:   assets/favicon.png
    new file:   assets/fonts/MavenProLight-200.otf
    new file:   assets/fonts/MavenProLight-300.otf
    new file:   assets/fonts/MavenProMedium.otf
    new file:   assets/fonts/maven_pro_bold-webfont.ttf
    new file:   assets/fonts/maven_pro_regular-webfont.ttf
    new file:   assets/icon.png
    new file:   assets/splash.png
    new file:   babel.config.js
    new file:   constants/colors.js
... (and all the rest of the files in my repository)

在项目根目录中仍存在 .git 目录,且大小超过 100MB。

Git 版本 2.24.3 (Apple Git-128)

运行 git reflog --all 命令的结果。

910641c4 (selectingDate) refs/heads/selectingDate@{0}: commit: Switching from many visibility animators to one translate animated view. Cuts down time to update list significantly.
f367a4d0 (refs/stash) refs/stash@{0}: WIP on SelectingDate: 37d06850 Ensures that restoreListener is readded to animatedDateValue when it is updated (and that any old one is removed).
e1437645 refs/stash@{1}: WIP on master: 193f904e Merge pull request #213 from bendelonlee/MobileGraphicCalNotes
f16f4e59 refs/stash@{2}: WIP on selectingDate: 37d06850 Ensures that restoreListener is readded to animatedDateValue when it is updated (and that any old one is removed).
37d06850 refs/heads/selectingDate@{1}: commit: Ensures that restoreListener is readded to animatedDateValue when it is updated (and that any old one is removed).
...

2
"git reflog" 的输出中可能会有一些线索。 - IMSoP
1
请问,你的 gsgc 别名究竟是什么? - Schwern
1
@BenjaminLee .git/logs/refs/heads/SelectingDate 存在吗?如果存在,最后几行是什么?您还可以尝试 git reflog --allgit log --graph --decorate --reflog --all 来查找丢失的提交。 - Schwern
4
翻译:仅猜测一下,您是否在使用不区分大小写的文件系统?参考日志输出和状态输出的大小写不同。 - anthony sottile
@AnthonySottile 我非常确定那就是问题所在。 - Benjamin Lee
显示剩余4条评论
2个回答

5

简述:请checkout selectingDate。

以下是具体情况。

你正在使用大小写不敏感的文件系统。这很重要,因为Git可以将分支名称存储为文件; .git/refs/heads/selectingDate 包含了你的 selectingDate 分支的提交ID。在某个时候,你执行了 git checkout SelectingDate 命令,它尝试打开 .git/refs/heads/SelectingDate 文件,但实际上打开的是 .git/refs/heads/selectingDate 文件。

这种方式有些问题。虽然 SelectingDate 可以匹配名为 selectingDate 的文件,但无法匹配像 .git/config 这样的文本,该文本可能包含针对 [branch "selectingDate"] 的设置。当前所检出的提交存储在 .git/HEAD 中,现在其中包含 ref: refs/heads/SelectingDate

然后发生 git gc,它会压缩你的引用。它将删除 .git/refs 中的所有单独文件,并将它们写入一个名为 .git/packed-refs 的文本文件中。现在分支名称区分大小写了!.git/HEAD 仍然显示你在 SelectingDate 上。Git 尝试将你的 checkout 返回到 "SelectingDate" 的提交上,但是在 .git/packed-refs 中找不到它的引用,而且 .git/refs/heads/selectingDate 也已经不存在了。所以 Git 认为它没有任何提交记录。

要解决这个问题,请checkout selectingDate。

如果 git branch 命令也显示 SelectingDate,请将其删除。

如果你不小心删除了两个分支,请不要慌张。分支只是标签。使用 git branch selectingDate 910641c4 命令来恢复分支。其中 910641c4 是你最后一次提交到 selectingDate 的提交ID。

参见


1
在收到您的回答之前,我已经备份了存储库副本,删除了所有文件,检出了主分支,然后检出了“selectingDate”分支。这似乎起作用了。 - Benjamin Lee
1
在备份副本中,只是为了看看会发生什么,我运行了 git reset --hard 910641c4。这也基本上起作用了。但有一个区别,在我执行reset的那个副本中,由 git log 报告的 Head 是:(HEAD -> SelectingDate, selectingDate) 而另一个副本的第一行是 (HEAD -> selectingDate) - Benjamin Lee
@Shwern,(HEAD -> SelectingDate, selectingDate) 对你来说是否有问题?我倾向于将删除、检出主分支、检出分支的方法作为被接受的答案,因为HEAD -> selectingDate) 不太奇怪...但是关于git,我还有很多不了解的地方。 - Benjamin Lee
1
从一些实验中可以看出,(HEAD -> SelectingDate, selectingDate)是因为您的引用已经被打包了。现在,.git/refs/heads/中的文件已经变成了.git/packed-refs中的行。现在它们是区分大小写的。如果您查看内部,您会看到SelectingDate和selectingDate都指向相同的提交。只需使用git branch -d删除您不想要的一个即可。 - Schwern
我运行了 git branch -d selectingDate,看起来它已经删除了两个分支...当然这不是什么大问题,因为它已经备份了。 - Benjamin Lee
1
@BenjaminLee 我本来以为 SelectingDate 是错的。无论如何,你可以使用 git branch selectingDate 910641c4 来恢复。分支只是标签。 - Schwern

1
我认为问题是,正如@asottle所猜测的那样,我在一个不区分大小写的文件系统上。我将分支命名为selectingDate,但将其检出为SelectingDate,这本不应该成功,但却成功了。也许在“自动打包”期间,git将我移到了不存在的' SelectingDate '分支。
如果这是您的问题,一种解决方案是备份存储库副本,从中删除所有已跟踪的文件(否则无法检出另一个分支,并且无法运行git stash,因为没有提交),检出主分支,然后检出具有与git最初知道的大小写相同的分支,例如selectingDate。我这样做了,我的最新提交仍然存在,我还没有注意到它引起的任何问题。
非常感谢@Shwern。

很高兴你解决了它!看起来Git会忽略大小写地检出,因为分支名称只是文件名。但如果分支名称在文本文件中,则不匹配。git checkout main将尝试打开.git/refs/heads/MAIN并打开.git/refs/head/main,但它不会匹配.git/config中的[branch "main"]。我有时会在区分大小写的磁盘映像上工作,以避免这些问题。 - Schwern

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接