git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: My git repo is broken, how to fix it ?

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Feb 28, 2007, 04:57 UTC
Message-ID
<Pine.LNX.4.64.0702272039540.12485@woody.linux-foundation.org>
In-Reply-To
<200702281036.30539.litvinov2004@gmail.com>
On Wed, 28 Feb 2007, Alexander Litvinov wrote:
> 
> Some times ago I descide to run fsck and found that by working repo is broken, 
> while imported repo is correct. Is there way to fix it ? 

Generally, the best way to fix things is (I've written this up at somewhat more length before, but I'm too lazy to find it):

 - back up all your state so that anything you do is re-doable if you 
   corrupt things more!
 - explode any corrupt pack-files
   See "man git-unpack-objects", and in particular the "-r" flag. Also, 
   please realize that it only unpacks objects that aren't already 
   available, so you need to move the pack-file away from its normal 
   location first (otherwise git-unpack-objects will find all objects 
   that are in the pack-file in the pack-file itself, and not unpack 
   anything at all)
 - replace any broken and/or missing objects
   This is the challenging part. Sometimes (hopefully often!) you can find 
   the missing objects in other copies of the repositories. At other 
   times, you may need to try to find the data some other way (for 
   example, maybe your checked-out copy contains the file content that 
   when hashed will be the missing object?).
 - make sure everything is happy with "git-fsck --full"
 - repack everything to get back to an efficient state again.

And remember: git does _not_ make backups pointless. It hopefully makes backups *easy* (since cloning and pulling is easy), but the basic need for backups does not go away!

> By the way, several times I interrupt git's commands like commit and pull 
> using Ctrl-C.

Shouldn't matter, at least as long as you are using the native git protocol: git will create objects fully under a temporary name, and then atomically rename things to their right names.

Using rsync and/or http may not be as safe.

HOWEVER! I do not know how well Windows and/or cygwin does file renames. If cygwin does a rename as a copy + delete, a lot of the safety assumptions just fly out the window.

Show 5 quoted lines
> I tried to unpack all objects:
>
> > git-unpack-objects -r < .git/objects/pack/pack-c4554978bbe079c9a43d6a13546a2fa314fe0884.pack; echo  $?
> Unpacking 12868 objects
>  100% (12868/12868) done

Ok, that's a good thing, but see above: I don't think anything should have gotten unpacked, because it found all objects already existing in the very pack-file you tried to unpack.

So you might well need to do
	mv .git/objects/pack/pack-c4554978bbe079c9a43d6a13546a2fa314fe0884.pack oldpack
	git-unpack-objects -r < oldpack
(or rename the .idx file instead).

Alternatively (and in many ways this migth be better when you're trying to recover something) just create a totally *new* git repo, by doing

	mkdir new-repo
	cd new-repo
	git init
	git unpack-objects -r < ../other-repo/.git/pack/pack-.....pack

and re-create the objects somewhere else - you can do all of this without at all disturbing the old repository (but you'd need to copy all the refs and all the loose objects by hand, of course!)

Show 9 quoted lines
> No erorts here. But fsck find that broken blob:
> > git fsck 
> dangling blob beb992198d4d8813ea51fd1cbbf38313ef490c22
> 
> git-cat-file shows me this this is a broken object with correct sha1 sum.
> 
> As a cunclusion: my repo has broken file and I don't see there is the brakage. 
> Can I reconstruct file by sha1 sum :-) or can I do something to stop fsck 
> warn me ?

You didn't do "--full", so it's not looking inside your pack, so the fsck wasn't very interesting in this case.

And no, you cannot reconstruct the file by sha1 sum, although you may be able to reconstruct the file some *other* way (by looking at the other blobs and remembering what the missing case is), and then you can obviously use the sha1-sum to *confirm* that you reconstructed the file exactly as it was!

So yes, reconstruction of missing objects is possible, but no, you can't do it based purely based on SHA1, you need to base reconstruction on some other information. That's kind of what "cryptographically secure hash" means ;^p

			Linus
Previous: Alexander LitvinovNext: Alexander Litvinov
Message 2 of 28 in “My git repo is broken, how to fix it ?”
  1. Alexander LitvinovFeb 28, 2007
  2. Linus TorvaldsFeb 28, 2007
  3. Alexander LitvinovFeb 28, 2007
  4. Linus TorvaldsFeb 28, 2007
  5. Alex RiesenFeb 28, 2007
  6. Alexander LitvinovMar 19, 2007
  7. Linus TorvaldsMar 19, 2007
  8. Linus TorvaldsMar 20, 2007
  9. Alexander LitvinovMar 20, 2007
  10. Junio C HamanoMar 20, 2007
  11. Nicolas PitreMar 20, 2007
  12. Linus TorvaldsMar 22, 2007
  13. Nicolas PitreMar 22, 2007
  14. Linus TorvaldsMar 22, 2007
  15. Linus TorvaldsMar 22, 2007
  16. Linus TorvaldsMar 22, 2007
  17. Nicolas PitreMar 22, 2007
  18. Linus TorvaldsMar 22, 2007
  19. Nicolas PitreMar 22, 2007
  20. Jeff KingMar 22, 2007
  21. Linus TorvaldsMar 23, 2007
  22. Bill LearMar 23, 2007
  23. Jeff KingMar 23, 2007
  24. git-apply: Do not free the wrong buffer when we convert the data for writeoutJunio C Hamano, Mar 22, 2007
  25. Linus TorvaldsMar 22, 2007
  26. Alexander LitvinovMar 23, 2007
  27. Alexander LitvinovMar 23, 2007
  28. Johannes SixtMar 22, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.