3

When you delve a bit into the internals of git (but haven't got as far as reading the codebase :) ), you come to understand that, when you run git add file.txt or git add dir/file.txt, an object is created for the file (a blob is written in git storage), but no objects are created for the directories (e.g: there is no tree object storing the content of the index, hierarchical levels included).

You can create that tree by calling git write-tree, but this is a separate action.

My question is:

Is there an ulterior motive behind that behavior ?

Are there some specific reasons why running git add doesn't also create tree objects straight ahead ?


On top of sheer curiosity, I'm also wondering what it would take to have a reflog for the index. If you had a tree, you could store a commit and a floating ref pointing to that tree, much like the stash does.

[update] The other issue I have in mind is: as it is, git add stores the content of a file in git storage, but not the file name, which is IMHO a bit odd.

4
  • I also understand the (good) reasons why git is very conservative about its commands behavior, perhaps there could be some flag to git add or some configuration setting to add this behavior ?
    – LeGEC
    Commented Mar 9, 2023 at 6:55
  • 1
    It might have to deal with the fact that you could have conflicts? If you created trees in the index, they would have to keep conflict state support in a structure that is thought of to be part of a commit without conflicts. That is what I have always assumed to be the reason why.
    – eftshift0
    Commented Mar 9, 2023 at 7:04
  • 2
    I think one of the possible reasons could be to reduce redundant objects as much as possible. A file could be edited and added multiple times before being committed. Along with the creation of a new blob, one or more tree objects, depending on the depth of the file, would be created at the same time. If we edit several files but add them one by one, how many tree objects would be just left over at last?
    – ElpieKay
    Commented Mar 9, 2023 at 8:22
  • I would ask little differently. Why would you want to create blob for folder? I mean, is there any reason to create snapshot of what folder contains, except the fact that it would be harder to have something in folder we don't want to have in repository? (or at least I assume it would be that way) And of course, we already have information of folder anyway in current approach, no need for redundancy as @ElpieKay mentioned.
    – kadewu
    Commented Mar 9, 2023 at 9:00

0

Browse other questions tagged or ask your own question.