修复损坏的 git repo - 如何重新开始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5859569/
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-19 05:24:06  来源:igfitidea点击:

Repair a broken git repo - how to have a fresh start

gitrepository

提问by Zac

I am somewhat of a newbie with git and was frustrated with how my repo was working.. or in this case not working.. so I deleted it off of git hub, deleted the git folder on the project and was planning to start fresh.

我是 git 的新手,对我的 repo 的工作方式感到沮丧。

Now when i try and set up a fresh repo I am getting a lot of messages like :

现在,当我尝试建立一个新的 repo 时,我收到了很多消息,例如:

warning: You appear to be on a branch yet to be born.
warning. Forcing checkout of HEAD

when I try and run the first commit I see :

当我尝试运行第一次提交时,我看到:

error: invalid object 8bcb4b5fd612e3ad55fb07e4bed087c55afd0861
fatal: git-write-tree: error building trees

I ran git fsck and see that I am missing a bunch of blobs.

我运行 git fsck 并看到我错过了一堆 blob。

How can I just wipe the slate clean and try again?

我怎样才能把石板擦干净然后再试一次?

采纳答案by sehe

Ok,

好的,

  1. have backups
  2. git clean -dfx .
  3. mv .git /tmp/_git.backup
  4. git init
  5. git add .
  6. git commit -m 'restart repo after rescue operation'
  1. 有备份
  2. git clean -dfx 。
  3. mv .git /tmp/_git.backup
  4. 混帐初始化
  5. git 添加。
  6. git commit -m '在救援操作后重启 repo'

Note the cleanstep that prevents a lot of previously untracked stuff from being added to the new repo. A good .gitignore goes a long way to prevent effects like that.

请注意clean防止将许多以前未跟踪的内容添加到新存储库的步骤。一个好的 .gitignore 对防止这种影响大有帮助。

If you want, you can manually migrate some/all of the old configuration, like

如果需要,您可以手动迁移部分/全部旧配置,例如

 vim -d _git.backup/config .git/config

(or simply copy it)

(或简单地复制它)

I'll not go into obvious details about impact on potential remote clones/branches :)

我不会详细介绍对潜在远程克隆/分支的影响:)

There are ways to reconcile things like that, but for now, this seems to answer your question.

有很多方法可以调和这样的事情,但就目前而言,这似乎可以回答您的问题。