开发者

Recovering broken git repository

开发者 https://www.devze.com 2022-12-08 17:48 出处:网络
My working git repository is broken, it lose track to all the files in it, i.e. $ git log fatal: bad default revision \'HEAD\'

My working git repository is broken, it lose track to all the files in it, i.e.

$ git log
fatal: bad default revision 'HEAD'  
$ git status  
... told me that all the files are new
开发者_如何转开发

However the .git directory does contain my objects.

$ du -sh .git
34M    .git
$ git count-objects
4151 objects, 32692 kilobytes
$ git --version
git version 1.6.0.4

The last thing I remember doing before it went wrong was creating (clone --mirror) a backup repository at an NFS-mounted server. However the backup repository cloned is broken the same way.

How can I restore my repository?


There must've been something besides the clone, but I know how hard it is to remember those things.

The first thing you want to do is look in .git/refs and see if there's anything valid in there (I'm not too optimistic since you say that there don't appear to be any branches, but it's worth a shot). If any valid refs exist, you may be able to get some information from git-reflog.

Next, I would start having a look at git-fsck. Its main purpose is to verify connectivity and validity of objects in the database. Depending on what exactly has happened to your repo, you may need --unreachable or --lost-found. Hopefully the objects are intact, so all you need to do is find some dangling commit hashes to check out and recreate branches at.


Try to check if each of your files in .git/ are owned by current user.

I had the same problem, when realizing I made some commits with root user, and that created objects (under .git/objects) where belongig to root, trigering errors when running git as regular user.

This command solved the problem:

sudo chown jb:jb .git/ -R *


You can examine manually, but that would require some knowledge on the format of the repository.

Without looking at the repository is hard to tell what is happening, but probably some file was corrupted.

Run git fsck and it will tell whether your repository is still valid.

Post the result of the git fsck run and that should help us to help you.


I got this problem just now after my GitHub application (PC) crashed. My branch disappeared when using git branch and it kept promting me to do my initial commit. I solved it by locating my branch in .git/refs/heads/ and renaming it from mybranch.lock to just mybranch (remove the lock).


I had this issue after a developer did a $ git init inside the bare master of a centralised repo.

If you're working with a repository with no working directory, check for a .git folder; deleting this should rectify the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号