3

Repairing a corrupt Git repo using a clone

 1 year ago
source link: https://edofic.com/posts/2016-02-24-git-repair/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Repairing a corrupt Git repo using a clone

2016-02-24

Quite recently I managed to make myself a corrupt git repository due to a file system failure. See, git stores everything in content addressable blobs - the file name of something is it’s hash. Which lends itself nicely to checking repository integrity - it keeps out malicious attackers as well as my file system problems.

I already hear you saying: Why not just make a new clone, git is distributed anyway? Well, I wasn’t diligent enough to push everything. I had local commits that were quite important, so I spent some time fixing it.

Git has a command to manually check integrity of the repository: git fsck. Running it lists all the errors.

 $ git fsck
error: garbage at end of loose object '3ce5b3af5d47179ff31a665ff267e1c7b6e4d8aa'
fatal: loose object 3ce5b3af5d47179ff31a665ff267e1c7b6e4d8aa (stored in .git/objects/3c/e5b3af5d47179ff31a665ff267e1c7b6e4d8aa) is corrupt

Luckily in my case the list was quite short so I went ahead and deleted all the objects that were listed as corrupted. So now my objects are fine, but I’m missing some. Luckily (again) corrupted objects did not contain any data pertaining to unpushed commits so I thought I can use a close to restore them.

unpack

So I lied a bit, git doesn’t store every blob in a separate file, that would become huge pretty quickly. Instead it uses packfiles. It packs several blobs into one file and does delta compression to reduce disk usage. So I cannot just copy over blobs from a clone.

Fortunately git has commands for dealing with packfiles as well. The one of interest is git unpack-file which takes a packfile, extracts all the blobs and dumps them into the repo. Potentially producing loose objects, but let’s not care about that for a second.

So I made a bare clone from github

git clone --bare [email protected]/edofic/blog

And just unpacked everything

cd ~/actual-blog-repo
git unpack-file < /tmp/blog.git/objects/pack/*.pack

And it worked! git fsck did not complain anymore. Well at least not about garbage and corruption - just loose objects.

But that is easy to clean up: just prune them

git prune --expire now

And do a GC to re-compress.

git gc

Any my repo integrity is back!


Last modified on 2016-02-24

Previous Lazy unary numbers
Next TDD-ing a toy sized project


Recommend

  • 11

    How to update a forked repo with git rebase – Git Many times you might came across to update/sync your forked repository with main repo. There are multiple ways but below one is very efficient way to sync your forked rep...

  • 4

    Configuring a Git Repo for CI / CD Style Pull Requests Oct 22, 2019 Like a lot of senior software folk, I have a particular se...

  • 9

    Repo coherence with git on NFS and multiple clients All of this talk about NFS got me thinking about an interesting failure mode I saw some time back. Someone else did the work and figured out how it all fit together. I remember...

  • 7

    Frida Dynamic instrumentation toolkit for developers, reverse-engineers, and security researchers. Learn more at frida.re. Two ways to install 1. Install from prebuilt binaries

  • 10
    • textslashplain.com 2 years ago
    • Cache

    Repairing Corrupt ZIP Files – text/plain

    Repairing Corrupt ZIP Files Fiddler’s default file format is the SAZ Format, which is just a ZIP file with a particular structure. Unfortunately, sometimes users’ SAZ files get corrupted due to failing disks or incompl...

  • 5
    • shenzilong.cn 2 years ago
    • Cache

    git repo star 列表&备注

    git repo star 列表&备注 star 了之后找又找不到,找到了也不记得当初为啥 star 了,将收藏这种信息放在...

  • 10

    Adventures using and repairing an X131e laptop from 2011 Adventures using and repairing an X131e laptop from 2011 As usual: prettiest picture is at the very bottom of the article.This article covers several repairs,...

  • 7
    • www.kevinhooke.com 2 years ago
    • Cache

    git tags and pushing to a remote repo

    git tags and pushing to a remote repo Tags are useful to mark specific versions or releases of your code in a repo on a given branch. To tag everything on the current branch, use: git tag new-tag-name

  • 4

    Your Opinion on My First Git Repo Hi devs, I've just created my absolutely first Github repo, and I wanted to get your opinion on it. The concerned project is a Nodejs SDK for web sc...

  • 1
    • opensource.com 2 years ago
    • Cache

    Peek inside your Git repo with rev-parse

    I use Git a lot. In fact, there's probably an argument that I sometimes misuse it. I use Git to power a flat-file CMS, a website, and even my

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK