【问题标题】:How to recover my staged files but not commited? [duplicate]如何恢复我暂存但未提交的文件? [复制]
【发布时间】:2018-01-11 01:40:17
【问题描述】:

我使用的命令:

  1. git add .(添加后我全部重置)
  2. git reset .
  3. git checkout .

在我检查了它们之后,我明白我需要它们。有什么方法可以恢复更改?

【问题讨论】:

标签: git github


【解决方案1】:

如果您的文件按您所说的那样暂存,您可以使用带有--lost-found 选项的git fsck 命令取回它们。

如果您运行 git fsck --lost-found,您将看到类似这样的内容(可能会有更多悬空的 blob 线):

Checking object directories: 100% (256/256), done.
dangling blob 84eab6f56e81cebe1356c9c2a6e2882c05f5fc01

其中一些悬空的 blob 可能是您丢失的文件。

如果您运行git show <SHA of a dangling blob>,您将能够看到该文件的内容,遗憾的是文件名可能会丢失。

但是,您可以将输出复制回适当的文件中。或者,在运行 git fsck --lost-found 之后,悬空的 blob 将保存到存储库根目录中的 .git/lost-found/other/ 目录中。

【讨论】:

    【解决方案2】:

    是的,如果您已暂存更改,那么您绝对可以取回您的文件。当您运行git add 时,文件实际上已添加到 Git 的对象数据库中。在您执行此操作的那一刻,git 会将文件放入索引中:

    % git add bar.txt
    % git ls-files --stage
    100644 ce013625030ba8dba906f756967f9e9ca394464a 0   bar.txt
    100644 6af0abcdfc7822d5f87315af1bb3367484ee3c0c 0   foo.txt
    

    请注意,bar.txt 的条目包含文件的对象 ID。 Git 实际上已将该文件添加到其对象数据库中。在这种情况下,Git 已将其作为松散对象添加到存储库中:

    % ls -Flas .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
    4 -r--r--r--  1 ethomson  staff  21 14 Jun 23:58 .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
    

    这些文件将最终被垃圾回收。但这将在 几个月 内发生,而不是几天。在这些文件被垃圾回收之前,您可以恢复它们。

    最简单的方法是在交互模式下download and install the git-recover program

    % git recover -i
    Recoverable orphaned git blobs:
    
    61c2562a7b851b69596f0bcad1d8f54c400be977  (Thu 15 Jun 2017 12:20:22 CEST)
    > Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    > tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
    > veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
    > commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
    
    Recover this file? [y,n,v,f,q,?]: 
    

    git-recover 在对象数据库中查找提交(或在索引中)的文件。您可以在the blog post announcing it 中找到有关git-recover 的更多信息。

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 2021-03-26
      • 2021-03-12
      • 2021-05-12
      • 2016-06-25
      • 2014-05-11
      • 2012-06-21
      • 1970-01-01
      相关资源
      最近更新 更多