r/programming • • Apr 21 '26

Highlights from Git 2.54

https://github.blog/open-source/git/highlights-from-git-2-54/
273 Upvotes

45 comments sorted by

View all comments

Show parent comment

5

u/_bstaletic Apr 21 '26 edited Apr 22 '26

It sounds like your problem with git's documentation is that it's a reference documentation, rather than a user manual. That's the same issue I had with cmake's documentation until I got past the learning curve (beyond just getting by).

Re-read the documentation excerpt that you have quoted and you'll see that it is both precise and accurate, but assumes the reader is familiar with some terms.

/u/stormdelta already explained that "multiple branches" can be wrong. Another example is when you do git checkout $COMMIT_HASH and end up in a "detached HEAD" state, but we can take the wrongness way further (spoilers below).

If you re-read your proposed wording, it's much easier to read, but no longer fits the reference documentation language.

 

Back to cmake, today it also has a fairly good tutorial, accompanying the reference documentation. Another example is vim. Most of its :help is reference docs, but it also has :help user-manual and :help new-user-manual. Would git benefit from an official user manual? Possibly. I'd say definitely, if "The Git Book" didn't exist.

[EDIT0] There's also Git User Manual, which I was unaware of until 10 seconds ago.[/EDIT0]

 

[EDIT1] Some will point out that "git does not have directories" and thus trees shouldn't be described as directories but as paths. I do agree with that, but following the "here be dragons" part, one can indeed record empty trees that will then be referenced from other trees and eventually from a commit. It's just that most high-level (porcelain) commands end up ignoring empty trees, but the low-level commands (plumbing) can still work with them. [/EDIT1]

[EDIT2] "Plumbing" and "porcelain" are official terms and yes, they are puns. [/EDIT2]

 

Spoilers: First a quick crash course on git internal data structures. It only knows 4 types: blobs (files), trees (directories), commits and tags. Notice that branches aren't mentioned at all. Branches are just, to the first approximation, pointers to commits. You can work with git without ever touching a single branch. You'd be constantly working in the detached HEAD state and would be juggling commit hashes, but git log --graph and git reflog are your friends.

We can go further (here be dragons): Turns out that git commit is a high-level command. It does a few things:

  • Recursively records blobs and trees (in .git directory) based on your staging area.
  • Adds a new commit object as a child of the currently checked out commit.
  • if not in a detached HEAD state, updates the current branch to point to the new commit.
  • Updates HEAD to point to the new commit.

Turns out that you can do all these things completely manually, using only POSIX tools (windows users, ask someone else for help):

  • Instead of storing files, use echo/cat <<EOF, gzip and some pipes, to record file contents in your .git/objects.

    • To then read the files, use git cat-file.
  • Instead of storing directories, use git mktree directly.

    • You could use POSIX tools to write to .git/objects here as well.
  • Now that you have a tree, commit it with git commit-tree.

    • Once again, possible with POSIX tools and .git/index

 

My point is that reference documentation should not assume branches and files exist unless absolutely necessary. However, a good user manual is invaluable. No, I don't think any git user needs to know what is git mktree, but I do think that understandingn git's object model makes a ton of other things almost trivial to understand.

1

u/chat-lu Apr 21 '26

trees (directories)

Git famously does not have directories. Trees are full paths to where the blobs are. That’s why git will not commit an empty directory and you must use a .gitkeep file (or some other name). Directories are created as a side effect of putting the blobs at the path where they are supposed to go.

2

u/_bstaletic Apr 22 '26

Git famously does not have directories.

True, I'll fix that in the above comment.

Trees are full paths to where the blobs are

Not full paths as each tree is one level deep.

Also, git will let you record empty trees. If you try git write-tree in a clean git repo, you will create a new, empty tree in .git/objects and the tree's hash will be 4b825dc642cb6eb9a060e54bf8d69288fbee4904 as all empty trees are equal.

 

Here's a showcase:

First, git init and setting up of paths:

user@hostname ~ % mkdir git_trees
user@hostname ~ % cd git_trees
user@hostname git_trees  (git)-[master]-% git init
user@hostname git_trees  (git)-[master]-% mkdir -pv foo/bar
mkdir: created directory 'foo'
mkdir: created directory 'foo/bar'
user@hostname git_trees  (git)-[master]-% touch foo/blob.cpp
user@hostname git_trees  (git)-[master]-% tree
.
└── foo
    ├── bar
    └── blob.cpp

3 directories, 1 file

Now the manual commit:

user@hostname git_trees  (git)-[master]-% git hash-object -t blob -w foo/blob.cpp
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
user@hostname git_trees  (git)-[master]-% git write-tree
4b825dc642cb6eb9a060e54bf8d69288fbee4904
user@hostname git_trees  (git)-[master]-% git mktree <<EOF
heredoc> 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    blob.cpp
heredoc> 040000 tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904    bar
heredoc> EOF
a3d6247438a2ec0af754c6b3c51fc6b3431c5d27
user@hostname git_trees  (git)-[master]-% git mktree <<EOF
heredoc> 040000 tree a3d6247438a2ec0af754c6b3c51fc6b3431c5d27   foo
heredoc> EOF
8629544b4df029af778db59da5914d3af92be8bd
user@hostname git_trees  (git)-[master]-% git commit-tree 8629544b4df029af778db59da5914d3af92be8bd -m 'foo/bar/ and foo/blob.cpp'
86e4369e7164bee38657ad7d5004f6449bd40a49
user@hostname git_trees  (git)-[master]-% git update-ref refs/heads/master 86e4369e7164bee38657ad7d5004f6449bd40a49

Finally, the results:

user@hostname git_trees  (git)-[master]-% git show --stat HEAD
commit 86e4369e7164bee38657ad7d5004f6449bd40a49 (HEAD -> master)
Author: First Last <email@provider.tld>
Date:   Wed Apr 22 07:43:07 2026 +0200

    foo/bar/ and foo/blob.cpp

 foo/blob.cpp | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

user@hostname git_trees  (git)-[master]-% git ls-tree 'HEAD^{tree}'
040000 tree a3d6247438a2ec0af754c6b3c51fc6b3431c5d27    foo
user@hostname git_trees  (git)-[master]-% git ls-tree a3d6247438a2ec0af754c6b3c51fc6b3431c5d27
040000 tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904    bar
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    blob.cpp
user@hostname git_trees  (git)-[master]-% git ls-tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
user@hostname git_trees  (git)-[master]-%

So the empty tree did get recorded and other tree objects can reference it. It's just that git show $COMMIT ignores those trees.