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

2019-03-27 07:36发布

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.

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

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.

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

1条回答
放荡不羁爱自由
2楼-- · 2019-03-27 08:21

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'

Note the clean step 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.

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.

查看更多
登录 后发表回答