How is it clear which of the files listed in a git tree are part of a specific commit?

46 Views Asked by At

A commit in git has a reference to a tree and that tree references blobs or other trees and captures a "snapshot" of the project.
What I am not clear about is how the association is done between a specific commit with the actual files affected by the commit.
Example in my project I have the files README and notes.txt.
If I add a new file e.g. contracts.txt and commit it I would have the new commit XYZ and if I did git show XYZ I would see only the file contracts.txt listed.
But if I do git cat-file -p XYZ I would see that its associated tree is ABC which doing git cat-file -p ABC would show:

blob F123 README  
blob A0F8 notes.txt  
blob DA03 contract.txt  

So I can't tell from this how could we figure out that from these 3 reported blobs reported as part of the tree, only DA03 contract.txt was added as part of the commit XYZ.
How is the association done?

2

There are 2 best solutions below

2
On

Simply: by comparing with the same information from the parent commit. For merges, the comparison is done with all the parents. Only blobs which don't come from either (thus are new files or new versions thereof; in the case of merges, also resolutions of merge conflicts) are shown by git show.

1
On

What I am not clear about is how the association is done between a specific commit with the actual files affected by the commit.

Git does not make such associations, it simply stores the snapshot of the repository for each commit. Diff tools display the files as new if they don't exist in the parent commit.