In GitHub URL's: what is the difference between a tree and a blob?
Asked Answered
S

3

46

I keep noticing this whenever I'm trying to add links to github files. You can add a link using blob:

https://github.com/facebook/pop/blob/master/Podfile

And the same doc comes up if you change blob to tree:

https://github.com/facebook/pop/tree/master/Podfile

What is the difference? Whenever I want to add a link for posterity in a doc, which one should I prefer?

Solfa answered 8/9, 2016 at 22:1 Comment(2)
Check this answer #5243346Tananarive
@JotaGe, that link describes the difference between a tree and a commit, not between a tree and a blob.Jurat
P
13

GitHub's website currently seems to be:

  • Using blob for files, and tree for directories, in URLs;
  • Redirecting browsers which request file URLs containing tree to contain blob instead; and
  • Redirecting browsers which request directory URLs containing blob to URLs containing tree instead.

It's possible that GitHub's website, at the time you asked the question, was only rewriting file URLs between tree and blob, instead of properly redirecting them. (Rewriting and redirecting URLs are activities of web servers.) If so, then the change wouldn't appear in your browser's address bar. But maybe you didn't mean that.

Paraffin answered 22/5, 2020 at 16:35 Comment(0)
H
12

I found this article helpful. This explains the git objects in detail.

Essentially, a blob is just a bunch of bytes that could be anything, like a text file, image, actual source code etc.

A tree is like a directory, it points to:

  • blob objects (how a directory points to other files)
  • other trees (how a directory can have subdirectories)
       tree

  /     |    \

blob   tree  blob

        |
      
       blob

Hopefully the above example clarifies the difference.

In your example, Podfile is a file containing source code. Hence, it is a blob object. However, git is smart and realizes this fact. Hence, when you click the link, it changes the tree in the link to blob. You can try and test this yourself by clicking the below tree link:

https://github.com/facebook/pop/tree/master/Podfile

Similary, if you go to a directory on a git repository it is a tree object. Again, if you change the tree to blob git is smart and realizes that it is actually a directory and not a file and changes the blob in the link to tree. Again, you can try and test this yourself:

https://github.com/facebook/pop/blob/master/pop-tests

In terms of which link to prefer when you want to add to a document, it depends on what does the link point to. Essentially, there are 4 types of git objects:

  • blob - file
  • tree - directory
  • commit - reference to tree
  • tag - reference to commit

Hope that answers your question. I still recommend going through the article to get a thorough understanding of git objects.

Haruspex answered 20/11, 2019 at 9:19 Comment(0)
V
4

A blob is a representation of a file, and file diffs are separated into contiguous modified chunks named hunks. Hunks are @@ delimited lines in the git diff output format.

A tree is a representation of a directory. There are different types of trees:

  • working tree

    The tree of actual checked out files. The working tree normally contains the contents of the HEAD commit tree, plus any local changes that you have made but not yet committed.

  • index

    A collection of files with stat information, whose contents are stored as objects. The index is a stored version of your working tree. Truth be told, it can also contain a second, and even a third version of a working tree, which are used when merging.

  • tree-ish

    A ref pointing to either a commit object, a tree object, or a tag object pointing to a tag or commit or tree object.

There are four types of objects in Git’s internal storage. Commit objects, annotated tag objects, blobs and tree objects.

References

Varga answered 28/8, 2017 at 18:4 Comment(1)
your answer is not helpful. I have the same questions and your answer did not address them.Hausa

© 2022 - 2024 — McMap. All rights reserved.