Zubin
Zubin

Reputation: 166

Does a git commit object always point to a top level tree object, or can it point to a "subtree" object?

As I understand it, a git commit object will always point exactly one tree object.

What I'm wondering is whether, in practice, this tree object is always guaranteed to be a git repository's top-level tree object, representing the entire state of my code at the point the commit object was created.

Is there any chance, in an actual git repository, that any commit object could point to a tree object lower down in the hierarchy?

Upvotes: 1

Views: 730

Answers (2)

DomQ
DomQ

Reputation: 4644

There is no difference between a "top-level" tree object and a subtree object in git; see The Git Object Model. Therefore, the theoretical answer is "yes". As Justin pointed out, there are indeed useful cases in practice for mixing and matching directory layouts in the same git repository — actually, git.git itself (git's own source depot) does this: its todo branch has a directory layout completely independent of the master branch.

Upvotes: 1

Justin W
Justin W

Reputation: 1315

Well, commits DO always point to the entire tree/branch history as it was at the time of commit, but you're not limited to having just one history in your repository...

You can have a branch for ProjectA and a branch for ProjectB (with completely different histories and sets of files) and using the subtree merge strategy, you can merge ProjectB's changes into a subdirectory of ProjectA, keeping the history of ProjectB attached! Now, those commits from ProjectB's branch history will still be in the root of their branch, but the merge commit will map them into a subtree of ProjectA. You can even re-merge when there are more commits in ProjectB to get the updates in ProjectA.

Pretty confusing thing to do though, if you ask me... This Pro Git section suggests merging without keeping the history from the other branch, which seems a lot saner.

Upvotes: 2

Related Questions