Home > Blockchain >  Can Git tree objects reference other tree objects?
Can Git tree objects reference other tree objects?

Time:08-04

I'm on the journey of learning how Git works internally, but there's something confusing about the tree objects that Git creates and stores.

From what I've read, they consist of a list of references, each of which points either to a blob or another tree. However, in practice, using the "porcelain" commands, I've never come upon a tree containing a reference to another one.

So, my question is, when would Git prefer to do such hierarchical referencing rather than directly referencing all the blobs that the particular snapshot consists of?

CodePudding user response:

A tree object in git represents a directory. If your worktree contains nested directories, then your git object representation of the repo will contain nested tree objects. So it is not git which "prefers to do such hierarchial referencing". Git simply stores the directory structure as nested tree objects.

The only way to to "avoid" nested trees, would be for you to commit all files in the root directory of the worktree.

CodePudding user response:

Of course they do:

$ git cat-file -p HEAD^{tree}
100644 blob 9b87ea1fc16e9ca86cd0cfed7c8015f40e16f93f    .clang-format
100644 blob 43967c6b20151ee126db08e24758e3c789bcb844    .cocciconfig
100644 blob a64d219137455f407a7b1f2c6b156c5575852e9e    .get_maintainer.ignore
100644 blob 4b32eaa9571e64e47b51c43537063f56b204d8b3    .gitattributes
100644 blob 265959544978a795516c321eec0659725a970a90    .gitignore
100644 blob 13e4f504e17fbb7f91d6f87ae5589b989157ee73    .mailmap
100644 blob a635a38ef9405fdfcfe97f3a435393c1e9cae971    COPYING
100644 blob 40d3c655b567a0b914fd9809dabcb8cdf5cc0f3c    CREDITS
040000 tree e2f8f04ae7d5c94391361dd8d1b08cbf837a85d9    Documentation
100644 blob fa441b98c9f6eac1617acf1772ae8b371cfd42aa    Kbuild
100644 blob 745bc773f567067a85ce6574fb41ce80833247d9    Kconfig
040000 tree 181359e2050ea072167a41bb08efa6d1e8821335    LICENSES
100644 blob 651616ed8ae25e8e790915721d86c6404cb171fa    MAINTAINERS
100644 blob 00fd80c5dd6e65bd332632dbc1ebb3c35015129f    Makefile
100644 blob 669ac7c32292798644b21dbb5a0dc657125f444d    README
040000 tree 6f3b1cc84c7eef8a53f5bace44ae460b9009f66b    arch
040000 tree f1b35dbbceb17b3405633cdf628a2fddc44dbbdf    block
040000 tree f1534d1891febdd0d7a8c167a5c909edb9e6da07    certs
040000 tree c8135c21bc07e28b245a55035e95b778bdb36ae1    crypto
040000 tree 9f430e0fd31d752d1809b01efe9c1193b798f746    drivers
040000 tree 3d282518541996863a001a07c7e77c25772def25    fs
040000 tree aa1cbae1ac8b70285682bf5cea0c2b9bade2a7df    include
040000 tree d22371b6bc2982467a303f603c7a76c93e89a865    init
040000 tree 5064557b536e083764cf98f0247ad28052a10aac    ipc
040000 tree 16b6933cdabf2123f21c8314d71013cb790697e5    kernel
040000 tree 4f857021eedcbaf58f340c374cb40c9c18462252    lib
040000 tree 71914e585b61f1808d25162075cb2bd2c7bed488    mm
040000 tree 8f3eab04697df870462a9f44f8021e6344af643b    net
040000 tree 343458ac1607f800a53510bc76333e83e8591e86    samples
040000 tree 73a4a478783a0f3fc39af89460710ac4b6acb321    scripts
040000 tree a91c84e6fcf19b3cdc4aa60a6035d5f82638cb70    security
040000 tree 740ba330baa0a199214803a74638115495a87b94    sound
040000 tree a6d1bb7314447d4e0582e3f14cd7843caf37d262    tools
040000 tree 2f26fb66b5b482ad022adc0b52b86c03f910d642    usr
040000 tree 4aea3b2e37368688f8d3fe79eec395270182774c    virt
$ git cat-file -p 6f3b1cc84c7eef8a53f5bace44ae460b9009f66b # one of those trees
100644 blob 756c19c34f99d2cbf0343dbe36d6a80c2507e75d    .gitignore
100644 blob fcf9a41a4ef5b15efaac04bc85c420a435603fb8    Kconfig
040000 tree a791f4e7dd2893a95004b75377184744010932c7    alpha
040000 tree 566e13db1e275c1f3b912257aa793311d302a1c5    arc
040000 tree 945ecd1e73d8b619505f00d55acefb305a39713f    arm
040000 tree af9dcfed8dcc78d042d79c41f2b7c8f7a775a9ff    arm64
040000 tree bf523ad29246395ff1dfc838f099c1ecbed4fe64    csky
040000 tree 9cdfc61d87e98b6ec23c3ea97827dbaab00de551    hexagon
040000 tree 22a8a2f41ffc57f3b432d34d18dcfaee15a18c3f    ia64
040000 tree e0a349a67b028537b3f0c0a29758655b9a57e398    loongarch
040000 tree 7445a92cde23dca7d5541e98706c23a65ae217f7    m68k
040000 tree 8d744e26a9f2fe86561eb4a7198f8864cf38f333    microblaze
040000 tree cce9ea750e3d8dd1f0e9032d54a224414fafe689    mips
040000 tree 3c7c8feb7ac3cabf6b24d5a1723cccd609c44d91    nios2
040000 tree d00367b4a09e0646cf92839e4aa567507b0ebabd    openrisc
040000 tree 01907f7b666d232210a805f6ab916cc5f10d2f85    parisc
040000 tree 8ec1feabf999a7352cf4026d3b7477dd51476941    powerpc
040000 tree 0c5e7563f4ea213783e68ebd80aa81ce8669a67d    riscv
040000 tree f7fb79e9caa10f16ab7dbcee74b661dcfd75c93e    s390
040000 tree 2fd39e307c5d296f82cb9fbd56434ee078f14d6b    sh
040000 tree e52506a886c3dada02f9699ce7390db1320d1ae7    sparc
040000 tree eabb74e0a9be74f4fc8afd9a2f5d67a48b27403d    um
040000 tree 7cc30ee44307a913bb8d316b2483bf7647db5a19    x86
040000 tree 8bdb9f395603501214cbbd699a181e33c4e8305f    xtensa

This is from the root tree of revision ca85855bdcae8f84f1512e88b4c75009ea17ea2f of linux.

  •  Tags:  
  • git
  • Related