git 如何删除“致命:松散的物体”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11597003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:11:20  来源:igfitidea点击:

How to remove "fatal: loose object"?

gitgithub

提问by fishtoprecords

One of my cloned repositories is getting this from a git fsck

我克隆的存储库之一是从 git fsck 获取的

fatal: loose object 40bda4e3b79c3d7bf598df31d9e68470f97a3f79 (stored in .git/objects/40/bda4e3b79c3d7bf598df31d9e68470f97a3f79) is corrupt

致命:松散对象 40bda4e3b79c3d7bf598df31d9e68470f97a3f79(存储在 .git/objects/40/bda4e3b79c3d7bf598df31d9e68470f97a3f79 中)已损坏

I've got another copy of it that fsck's cleanly.

我有另一个 fsck 的副本。

I've tried nuking the directory/subdirectories that contain the fatal one, and recloning it. The problem continues.

我试过 nuking 包含致命目录的目录/子目录,并重新克隆它。问题还在继续。

I really don't care about any particular file, I just want the repository to checkout cleanly. What do I do?

我真的不关心任何特定的文件,我只希望存储库干净地签出。我该怎么办?

Note: the remote repository is hosted on github.

注意:远程存储库托管在 github 上。

回答by Seth Robertson

Easy answer: move the old repo away and reclone. If you have stuff in the old repo you want to preserve, there are ways of getting them, but first get a good repo.

简单的答案:将旧的回购移走并重新克隆。如果您想保留旧仓库中的内容,可以通过多种方式获取它们,但首先要获得一个好的仓库。

回答by Hustlion

The idea is:

这个想法是:

  1. first remove your current git records which reside in your .gitunder your current project (let's denote it by "project A").
  2. Make copy of your current project, we denote the duplicate as "project B".
  3. Now cdinto project A and git clone and reset to the last commit.
  4. Copy all the files from project B and replace those in project A.
  5. Now cdinto project A and use git statusto see the status of project A. It should be just as if there was no corruption happened ever.
  1. 首先删除驻留在.git当前项目下的当前 git 记录(让我们用“项目 A”表示)。
  2. 复制您当前的项目,我们将副本表示为“项目 B”。
  3. 现在cd进入项目 A 和 git clone 并重置为最后一次提交。
  4. 复制项目 B 中的所有文件并替换项目 A 中的文件。
  5. 现在cd进入项目 A 并使用git status查看项目 A的状态。应该就像从来没有发生过任何损坏一样。

In short, make a copy to reserve your current working area, and merge it with a fresh clone by replacing files and then deal with your changes as normal.

简而言之,制作一个副本以保留您当前的工作区域,并通过替换文件将其与新克隆合并,然后照常处理您的更改。

Here's it in action:

这是它的实际操作:

  1. First I see I have a corruption (which is because I forced my virtual machine to shutdown inproperly yesterday :P):

    ? domicor@ubuntu ~/dotfiles master ● git status error: object file .git/objects/cd/593f6db1d5050406e15b9c80d215fd48db39f4 is empty error: object file .git/objects/cd/593f6db1d5050406e15b9c80d215fd48db39f4 is empty fatal: loose object cd593f6db1d5050406e15b9c80d215fd48db39f4 (stored in .git/objects/cd/593f6db1d5050406e15b9c80d215fd48db39f4) is corrupt corrupted

  2. Make a copy of my dotfilesfolder and now remove the .gitfolder.

    $ cd ~/dotfiles $ rm -rf .git

    dotfiles

  3. Then recreate a git repo.

    $ git init

  4. Add a remote.

    $ git remote add origin [email protected]:domicor/dotfiles.git

  5. Fetch from the remote.

    $ git fetch

    Here's a screen shot of the above commands:rm-clone-fetch

  6. Now reset the HEAD.

    $ git reset --hard origin/master

    This is the shot: reset

    Notice that right after I fetched from remote I issued a git statuscommand to check out the status of my repo. You can see all the files are untracked. And the lst part of the command line prompt is orange from my git initcommand to git resetcommand indicating that when I initialized my repo, git detected files and after the reset, all files are restored to the state of the HEAD thus the green prompt.

  7. Set the upstream branch:

    $ git branch --set-upstream-to=origin/master master

  8. Now copy your files from the backup folder (for me it's dotfiles (copy)) manually or through the command line to replace files in the dotfilesfolder. Check out the status and there should be changes just like my screen shot:

    final

  9. Now you can git diff your-filenameto check out if your changes are applied properly and you can add files and commit from now on. \o/

  1. 首先我看到我有一个损坏(这是因为我昨天强迫我的虚拟机不正常关闭:P):

    ? domicor @ ubuntu的〜/点文件主●git的状态错误:对象文件的.git /对象/ CD / 593f6db1d5050406e15b9c80d215fd48db39f4是空的错误:对象文件的.git /对象/ CD / 593f6db1d5050406e15b9c80d215fd48db39f4是空的致命:松散对象cd593f6db1d5050406e15b9c80d215fd48db39f4(存储在的.git /对象/cd/593f6db1d5050406e15b9c80d215fd48db39f4) 已损坏 损坏

  2. 复制我的dotfiles文件夹,现在删除该.git文件夹。

    $ cd ~/dotfiles $ rm -rf .git

    点文件

  3. 然后重新创建一个git repo。

    $ git初始化

  4. 添加遥控器。

    $ git remote add origin [email protected]:domicor/dotfiles.git

  5. 从远程获取。

    $ git 获取

    这是上述命令的屏幕截图:rm-clone-fetch

  6. 现在重置 HEAD。

    $ git reset --hard origin/master

    这是镜头: 重启

    请注意,在我从远程获取数据后,我立即发出了一个git status命令来检查我的 repo 的状态。您可以看到所有文件都未跟踪。命令行提示符的第一部分从我的git init命令到git reset命令是橙色的,表示当我初始化我的 repo 时,git 检测到文件,并且在重置后,所有文件都恢复到 HEAD 的状态,因此出现绿色提示。

  7. 设置上游分支:

    $ git branch --set-upstream-to=origin/master master

  8. 现在dotfiles (copy)手动或通过命令行从备份文件夹(对我来说是)复制文件以替换文件dotfiles夹中的文件。查看状态,应该有变化,就像我的屏幕截图一样:

    最终的

  9. 现在您可以git diff your-filename检查您的更改是否正确应用,您可以添加文件并从现在开始提交。\o/

To understand it furthur, the git VCS just records your changes. When the VCS is corrupted, your files are still safe. They are still there as you left them. So you backup your files and restore the git records from elsewhere and git can compare the restore copy with your current file status and have ideas about what has changed just as it did before the corruption. And cheers \o/

为了进一步理解它,git VCS 只记录您的更改。当 VCS 损坏时,您的文件仍然安全。当你离开他们时,他们还在那里。因此,您备份文件并从其他地方恢复 git 记录,并且 git 可以将恢复副本与您当前的文件状态进行比较,并了解发生了什么变化,就像它在损坏之前所做的那样。和欢呼\o/

回答by Slam

First, you can check the file system for errors: fsck -y

首先,您可以检查文件系统是否有错误: fsck -y

Then, check the git repository: git fsck

然后,检查 git 存储库: git fsck

回答by Rameez Rami

I faced this issue and this simple trick solved the issue.

我遇到了这个问题,这个简单的技巧解决了这个问题。

Step 1

第1步

Suppose you are facing this issue in a folder named "my-project" with branch "my-new-feature", clone the repo to a new folder named "my-project-dummy" and checkout to "my-new-feature"

假设您在名为“my-project”且分支为“my-new-feature”的文件夹中遇到此问题,将存储库克隆到名为“my-project-dummy”的新文件夹中,然后结帐到“my-new-feature”

Step 2

第2步

Delete ".git" folder in "my-project" folder and copy ".git" folder from "my-project-dummy" folder to "my-project"

删除“my-project”文件夹中的“.git”文件夹并将“.git”文件夹从“my-project-dummy”文件夹复制到“my-project”

Tadaaa you are good to go now.

Tadaaa你现在可以走了。

回答by Bhargav Yadati

Do git checkoutto the other branch, you will get list of changed files , just commit them and push into the branch. If you do this the problem will be solved, next time when you do some changes git statusshould work.

git checkout其他分支,你会得到修改过的文件列表,只需提交他们推入分支。如果您这样做,问题将得到解决,下次您进行一些更改时git status应该会起作用。

回答by bo858585

Simplest answer is "rm -rf .git ..." http://www.bazhukov.net/2015/02/git-corrupt-loose-object/

最简单的答案是“rm -rf .git ...” http://www.bazhukov.net/2015/02/git-corrupt-loose-object/