【问题标题】:Is possible to push the non-pushed commits if the git files contain corrupt loose objects?如果 git 文件包含损坏的松散对象,是否可以推送未推送的提交?
【发布时间】:2014-01-01 23:42:31
【问题描述】:

git status 输出以下内容:

# On branch master
error: corrupt loose object '1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d'
fatal: loose object 1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d (stored in .git/objects/1e/2dc6d8b6eec682f3ecc9dd879445fea5d6a34d) is corrupt

我有 12 个未推送的提交。我可以在git log 看到它们。

有没有希望推动他们?

git push 输出:

error: corrupt loose object '1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d'
fatal: loose object 1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d (stored in .git/object/1e/2dc6d8b6eec682f3ecc9dd879445fea5d6a34d) is corrupt
error: failed to push some refs to 'git@bitbucket.com:username/reponame.git'

在我运行这些损坏的文件之前:

find . -type f -print0 | xargs -0 sed -i 's/\s\+$//g'

(在 git repo 目录中)

这影响了.git 文件。


更新:我尝试了 VonC's suggestion 运行以下命令:

cd ~
$ git clone git@bitbucket.org:username/reponame.git
Cloning into 'reponame'...
remote: Counting objects: 1561, done.
remote: Compressing objects: 100% (853/853), done.
remote: Total 1561 (delta 966), reused 1067 (delta 642)
Receiving objects: 100% (1561/1561), 223.80 KiB | 94.00 KiB/s, done.
Resolving deltas: 100% (966/966), done.
Checking connectivity... done
$ cd reponame/
$ $ git remote add yourLocalRemoteName /home/user/corupted/git/repository/folder
$ git pull yourLocalRemoteName
fatal: attempt to fetch/clone from a shallow repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

【问题讨论】:

  • 我已经编辑了答案以反映我们讨论的内容。

标签: git recovery


【解决方案1】:

你可以试试:

  • 再次克隆您的 BitBucket 存储库

  • 在新克隆中添加一个引用当前本地克隆的远程

      git remote add corrupt/path/to/your/first/cloned/repo
    
  • git pull corrupt,然后是git push


话虽如此,使用git fsck进行确认后,您可以试试solve that corrupt object
像往常一样,首先备份该本地存储库。


discussion之后,原来是:

  • 损坏的 repo 很浅(由工具自动克隆)。
    这意味着您可以轻松地克隆它,即使您使用rm .git/shallow,如suggested here

  • 解除浅层 repo 的一种方法是到 unpack a fresh repo 并将 .git/objects/pack 文件夹复制到损坏的 repo:

      git clone git@bitbucket.org:username/reponame.git repo2
      mkdir packs
      cp repo2/.git/objects/packs/* packs
      cd repos2
      git unpack-objects < ../packs/*.pack
      cd ..
      cp -r repo2/.git/objects/* corrupt/.git/objects/
    
  • 这将消除由于 sed 命令损坏的旧版解包对象导致的任何错误,但这不会修复自克隆以来所做的错误。

对于损坏的 repo,常用命令列在 this answerthis comment
最后,OP 有:

git fsck --full
error: corrupt loose object '3ddb67eb5343e75ac36656c86c4386f8dd117df4'
fatal: loose object 3ddb67eb5343e75ac36656c86c4386f8dd117df4 (stored in .git/objects/3d/db67eb5343e75ac36656c86c4386f8dd117df4) is corrupt

(不再有:

error: inflate: data stream error (incorrect data check)
error: sha1 mismatch ...

这是由于遗留提交损坏)

为了解决这个问题,可以尝试关注“Documentation/howto/recover-corrupted-blob-object.txt”。


话虽如此,如果没有成功,至少将所有最后的提交归为一个会更容易:

git clone git@bitbucket.org:username/reponame.git repo2
cd repo2
git --work-tree=../corrupt add -A .
git commit -m "last changes"
git push

可以尝试重建所有 12 次提交,但这并不明显,尤其是在一个文件中进行多项修改时。

【讨论】:

  • 你能解释一下yourLocalRemoteName是什么意思吗?
  • 当我运行git pull yourLocalRemoteName: * fatal: attempt to fetch/clone from a shallow repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. * 时出现此错误
  • @IonicăBizău 这只是您遥控器的名称。将其替换为您想要的任何名称,例如 repo1
  • @IonicăBizău 你在第二个本地克隆仓库中做了git remote add repo1 /local/path/to/your/first/repo 吗?
  • 是的,我做了...也许.git文件夹的路径是必需的?
猜你喜欢
  • 1970-01-01
  • 2011-05-14
  • 2019-11-10
  • 2015-05-13
  • 2013-12-17
  • 1970-01-01
  • 2011-01-02
  • 2017-12-22
  • 2019-08-03
相关资源
最近更新 更多