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

Re: git behaviour question regarding SHA-1 and commits

From
Jeff King <peff@peff.net>
Date
Nov 14, 2011, 11:48 UTC
Message-ID
<20111114114856.GF10847@sigill.intra.peff.net>
In-Reply-To
<7vwrb3l6v2.fsf@alter.siamese.dyndns.org>
On Sun, Nov 13, 2011 at 07:29:05PM -0800, Junio C Hamano wrote:
Show 15 quoted lines
> If the collision is between commit objects, for example, we would write
> the (old) commit object name to the tip of the current branch. Most
> likely, the tree object recorded in the (old) commit would not match the
> tree object your "git commit" wanted to record (otherwise you have hit
> SHA-1 collision twice in a row ;-), which would mean "git status" would
> show that a whole bunch of paths have changed between the HEAD and the
> index. Also "git log" would show the history leading to the (old) commit
> that is likely to be very different from what you would expect immediately
> after committing the collided commit. Of course, you could recover from it
> with "git reset --soft" after finding out what the previous HEAD was from
> the reflog, but it won't be a pleasant experience.
> 
> There can be other kinds of collisions (e.g. your latest commit might have
> collided with an existing blob or tree, in which case it is likely that
> almost nothing would work after finding a blob or tree in HEAD).

You are more likely to just have blobs collide, since we generate many more blobs than commits (each commit should have at least one changed blob, but typically has more).

And in that case, I expect git would silently lose that state. We would fail to write the new blob to the object db, but "git diff" would report nothing, as it would see that the index entry's sha1 is the same as what is in HEAD, and that the file is up to date with respect to the stat information in the index. So if you were to "git checkout", your content would be lost forever. However, if you instead modify the file further, the new content will be kept (and you will get a very confusing diff).

-Peff
Previous: Junio C HamanoNext: Jonathan Nieder
Message 4 of 11 in “git behaviour question regarding SHA-1 and commits”
  1. vinassa vinassaNov 13, 2011
  2. Ævar Arnfjörð BjarmasonNov 13, 2011
  3. Junio C HamanoNov 14, 2011
  4. Jeff KingNov 14, 2011
  5. Jonathan NiederNov 13, 2011
  6. vinassa vinassaNov 13, 2011
  7. Jeff KingNov 14, 2011
  8. Victor EngmarkNov 14, 2011
  9. Jeff KingNov 14, 2011
  10. Dmitry PotapovNov 13, 2011
  11. Johannes SixtNov 14, 2011

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.