git: a commit is a snapshot, a branch is one hash in a file
Inside .git: a commit is a snapshot of the whole project, a branch is a 41-byte file, plus HEAD, fast-forward merge and git revert on a real repo.
🌐 Oʻzbekcha · Русский · English
Lesson GIT-1.01 · Git va GitHub: 0 → Pro · ▶️ video coming soon · 💻 GitHub
This is the full written version of the video lesson: you can learn from it without watching (or hearing) the video. The video itself is in Uzbek.
What you will learn
You type git add, git commit and git push every day. Question: when you run git branch chegirma, does git
copy the whole project? Today we check this inside the .git folder. The answer: just one new file appears in
.git/refs. It holds a 40-character hash and an end-of-line character, 41 bytes in total.
One idea: a commit is a snapshot of the whole project plus a reference to the previous commit, and a branch is usually a small file with one commit hash in it. There is no magic inside git: only objects named by their hash and small files that reference them.
By the end of the lesson you will be able to:
- open commit, tree and blob objects with
git cat-file -pand read every line of them; - check for yourself where a hash comes from, with
git hash-objectandsha1sum; - show which file a branch and
HEADlive in, and how a fast-forward merge moves that file; - undo a mistake that reached
mainwithgit revertwithout breaking history, and know what revert does not do.
Today’s three core new words:
| Word | Plain meaning | English term |
|---|---|---|
| snapshot | the state of the whole project at the moment of the commit, not a list of changes | snapshot |
| object | an entry in .git/objects named by the hash of its own content: blob (file content), tree (folder), commit | object (blob, tree, commit) |
| ref (reference) | a name that holds a commit hash: a branch is such a ref, and HEAD is usually a reference to a branch | ref (reference) |
Helper words get one sentence where they first appear: parent (the previous commit), HEAD (where you are now), fast-forward (moving a branch file forward), revert (an opposite commit).
Before you start
- SYS-0.01 · Terminal: pipes,
grepand jobs you solve in one line: the vertical bar (pipe)|. Today we use it infind … | sort,printf … | git hash-object --stdinand… | od. - SYS-0.02 · Processes and the environment:
ps,kill,$PATHand env variables: an environment variable set withexport. This is how we fix the commit time (vaqt.sh).
We assume you know the git commands (add, commit, push). In this lesson we look at what they write inside
.git.
How to run the code. You need git 2.29 or newer, bash, and Go (Go only for go run main.go). On Windows use
Git Bash: in PowerShell 5, > writes the file in UTF-16 and a hand-written ref breaks. Download the course
repository and go to the lesson folder (cd changes the terminal’s current folder):
git clone https://github.com/abdurahmon27/go-backend-uz
cd go-backend-uz/lessons/git-01-commit-snapshotThe commands on this page are written as in the video and the README: each one starts from the lesson folder, so
most of them start with cd ombor && …. In a terminal, cd stays in effect: if you are already inside ombor,
leave out the cd ombor && part (or run cd .. first). Run the commands in the order of the page: each one
continues from the state the previous one left. To start over, run ./qur.sh again.
A commit hash is computed from the author, the time and the content. qur.sh builds the practice repository with a
fixed name and time, so every hash on this page should match yours character for character. In your own repository
the hashes will be different, and that is normal.
First, a question from SYS-0.01. If you give a command after the vertical bar no file, where does it read its data from?
A) from the current folder · B) from standard input (stdin), that is, from the pipe · C) from nowhere, it fails
Answer
B. A command with no file reads standard input, and the pipe sends the output of the command on the left exactly
there. Today we write printf 'non: 3000\n' | git hash-object --stdin: the --stdin flag tells git hash-object
“take the text from standard input, not from a file”. “A” was a property of grep -r (without a file, -r reads the
current folder), not a general rule.
A new branch: a 41-byte file
First we build the practice repository. We go through qur.sh line by line in the next sections. For now the output
is enough: two commits, the newest on top.
$ ./qur.sh
daac3a4 narx dasturi
e657f41 READMENow we create a branch called chegirma (“discount”). What appears on disk?
A) a full copy of the project · B) a new folder inside .git with copies of the objects · C) one small file
Answer
C. git branch chegirma writes only the file .git/refs/heads/chegirma. Inside is the full hash of the current
commit. If you chose “A” or “B”, you thought a branch is a copy of the project. In git a branch copies nothing.
$ cd ombor && git branch chegirma && find .git/refs -type f | sort && cat .git/refs/heads/chegirma
.git/refs/heads/chegirma
.git/refs/heads/main
daac3a47cb72faf965c5385ab1002bc04bf89b45find .git/refs -type f lists every file inside .git/refs, and sort puts them in alphabetical order. Two files:
main (it was there before) and the new chegirma. Inside chegirma is a 40-character hash: the commit daac3a4,
the first line that ./qur.sh printed. Let’s measure the file:
$ cd ombor && wc -c .git/refs/heads/chegirma && tail -c 3 .git/refs/heads/chegirma | od -An -c
41 .git/refs/heads/chegirma
4 5 \nwc -c counts bytes: 41. tail -c 3 takes the last 3 bytes of the file, and od -An -c shows them character by
character (-An means no address column on the left). The last three bytes are the last two characters of the hash,
4 and 5, and the end of line \n. So 40 characters + 1 = 41 bytes. That is the whole branch.
This holds for the default settings: in a SHA-256 repository the hash has 64 characters and the file is 65 bytes, and in the future reftable format refs are not stored in separate files at all (more in “A branch by hand”).
A commit is not a diff, it is a snapshot
Many developers think of a commit as a list of changes, a diff: “this commit changed one line in main.go”. In
fact git stores a snapshot of the whole project in every commit. The Pro Git book puts it this way: git thinks of
its data as “a series of snapshots of a miniature filesystem”.
The diff that git show or git diff prints is not stored in the commit. Git computes it again each time, between
two snapshots.
Next to the snapshot, a commit also has:
- the author and the time;
- the message (
-m "…"); - the parent: the hash of the previous commit.
The frontend parallel: in React you don’t change state in place, you pass a new state object each time. The difference between two states can be computed when you need it. Git commits work the same way: each one is a full, unchanging state, and the difference is something you compute. One difference: git gives every file and folder in the snapshot a name, the hash of its content. We will see what that leads to in “Is an unchanged file stored again?”.
How the practice repository is built: qur.sh
git init creates an empty repository with a .git folder inside. git add prepares a file for the next commit.
git commit takes the snapshot and writes it to history. The script uses these three commands; the rest makes the
hashes the same everywhere.
#!/usr/bin/env bash
# ombor/ ni noldan quradi: ikkita commit. Muallif va vaqt qat'iy (vaqt.sh). git >= 2.29.
set -e; cd "$(dirname "$0")"; rm -rf ombor; mkdir ombor; cd ombor
rf=--ref-format=files; git init -h 2>&1 | grep -q ref-format || rf= # eski git'da bu bayroq yo'q
git init -q --template= -b main --object-format=sha1 $rf
git config user.name Lola
git config user.email lola@example.com
git config commit.gpgsign false; git config core.hooksPath /dev/null # imzo va hook’lar hash’ni o’zgartiradi
git config core.pager cat; git config color.ui false; git config core.abbrev 7
git config core.autocrlf false; git config core.excludesFile /dev/null; git config core.attributesFile /dev/null
git config log.decorate false; git config branch.sort refname
git config merge.ff true; git config merge.stat true
cp ../fayllar/README.md .
git add README.md
. ../vaqt.sh; git commit -q -m "README"
cp ../fayllar/main.go .
git add main.go
. ../vaqt.sh; git commit -q -m "narx dasturi"
git log --onelineThe comments in the scripts are in Uzbek because the course code is shared. ombor means “storehouse” (the practice
repository folder), fayllar means “files”, and narx dasturi means “price program”.
| Line | Code | What it does |
|---|---|---|
| 1 | #!/usr/bin/env bash | the script runs with bash |
| 3 | set -e; cd …; rm -rf ombor; mkdir ombor; cd ombor | stop at the first error; go to the script’s folder; delete the old ombor and create an empty one |
| 4 | rf=--ref-format=files … || rf= | quietly (-q) looks for the word ref-format in the help text of git init -h (2>&1: together with stderr): if found (git 2.45+), refs are kept in plain files; if not (||), rf is empty |
| 5 | git init -q --template= -b main --object-format=sha1 $rf | empty repository and .git: first branch main, SHA-1 hashes, no template files |
| 6–7 | git config user.name / user.email | the author’s name and email, for this repository only |
| 8 | commit.gpgsign false, core.hooksPath /dev/null | commit signing and hooks are off: they can change the commit content, and so the hash |
| 9–12 | core.pager cat, color.ui false, core.abbrev 7, … | fix how output looks: no pager and no colour, short hashes of 7 characters; line endings, ignore and attributes files from your global settings are ignored; git log without decorations, git branch sorted by name, merge may fast-forward and prints statistics |
| 14–16 | cp …README.md ., git add, . ../vaqt.sh; git commit | first snapshot: only README.md |
| 18–20 | the same with main.go | second snapshot: README.md + main.go |
| 21 | git log --oneline | the two lines above: short hash and message |
vaqt.sh (“time”) fixes the time:
# Manba qiling: `. ../vaqt.sh` (ombor/ ichidan). Commit vaqtini qat'iy qiladi:
# 2026-01-05 10:00 (+05:00) va har bir mavjud commit uchun +10 daqiqa.
# Shuning uchun commit hash'lari sizda ham, bizda ham bir xil chiqadi.
# muallif repo sozlamasidan olinsin (user.name/email), muhitdagi qiymatdan emas
unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
n=$(git rev-list --all --count 2>/dev/null || echo 0)
t=$((1767589200 + n * 600))
export GIT_AUTHOR_DATE="@$t +0500" GIT_COMMITTER_DATE="@$t +0500"- Line 5: if the environment has a variable like
GIT_AUTHOR_NAME, it is removed, so the name comes from the repository settings. - Line 6: the number of commits in the repository (
n).$(…)takes a command’s output as a value; if there are no commits yet (||),0. - Line 7:
$((…))is shell arithmetic.1767589200is 2026-01-05 10:00 Tashkent time in Unix seconds; +600 seconds (10 minutes) per commit. - Line 8: git takes the time from these two environment variables. After
@comes the Unix time;+0500is the offset from UTC.
. ../vaqt.sh (a dot and a space) runs the script in the current shell, so the values set with export reach the
next git commit. bash vaqt.sh would run in a separate process, and the environment would disappear with it
(SYS-0.02).
In your own repository you need none of this: you write your name and email once, then use a plain git commit.
git config --global user.name "Ism"
git config --global user.email "siz@example.com"What is inside a commit
Now we open the commit. git cat-file -p prints an object in readable form (-p is pretty-print). HEAD is
where you are now, that is, the last commit of the current branch.
$ cd ombor && git cat-file -p HEAD
tree 651f29e7e2b98f1f8f5b84a4051ff7e85e41e3fb
parent e657f417275efcc319ccce4e1badded9976a8fa6
author Lola <lola@example.com> 1767589800 +0500
committer Lola <lola@example.com> 1767589800 +0500
narx dasturi| Line | What it is |
|---|---|
tree 651f29e… | the snapshot of the project folder: the hash of a tree object (we open it in the next section) |
parent e657f41… | the 40-character hash of the previous commit. This reference links commits into a chain |
author Lola … 1767589800 +0500 | who wrote the code and when: Unix seconds plus the offset from UTC (+5 hours, Tashkent time): 2026-01-05 10:10 |
committer … | who created the commit. Usually the same as the author; different when you apply someone else’s patch |
empty line, narx dasturi | the commit message |
No diff: inside a commit there is only the snapshot (tree), the parent, who, when and why. We see the whole chain with
git log. %h is the short hash, %p the parent’s short hash, %s the message:
$ cd ombor && git log --format='%h parent: %p %s'
daac3a4 parent: e657f41 narx dasturi
e657f41 parent: READMEThe first commit has no parent: it is the start of the chain.
tree and blob
A tree has one line for each file or subfolder. 'HEAD^{tree}' means “the tree of the HEAD commit”; the quotes
stop the shell from touching {}.
$ cd ombor && git cat-file -p 'HEAD^{tree}'
100644 blob 51c4fee9be20d18260ace91ce44e4b9c0ca396ab README.md
100644 blob 12b6d3bcf6f52e8be1d6f34b155db13dfbc46215 main.goEach line has four parts: the mode, the object type, the hash and the name. The six-digit mode is not a set of permissions but the file type:
| Mode | Meaning |
|---|---|
100644 | a normal file (both of ours) |
100755 | an executable file |
120000 | a symlink |
040000 | a subfolder: this line points to another tree |
The file content lives in an object called a blob. HEAD:main.go means “main.go in the tree of HEAD”:
$ cd ombor && git cat-file -p HEAD:main.go
package main
import "fmt"
func main() { fmt.Println("non: 3000 so'm") }The file itself came out (non: 3000 so'm means “bread: 3000 sum”). A blob has no file name and no time, only the
content: the name is in the tree. Objects are stored on disk compressed with zlib, which is why we read them with
git cat-file, not cat.
Both commits and their objects in one picture:
.git/refs/heads/main
│
▼
commit daac3a4 "narx dasturi" ──parent──► commit e657f41 "README"
│ tree │ tree
▼ ▼
tree 651f29e tree 19c7d67
├── README.md ──► blob 51c4fee ◄───────── └── README.md
└── main.go ──► blob 12b6d3bIs an unchanged file stored again?
The pause question from the video. README.md was added in the first commit and did not change in the second. Both
commits are full snapshots. Does git store README once more for the second snapshot?
A) yes, every snapshot stores its own copy · B) no, both trees reference one blob · C) no, because the second commit stores only the diff
Answer
B. The hash is computed from the content. Same content, same hash, so the object is the same one. The second tree references that blob and no new copy is made. If you chose “A”, you thought snapshot = copy. “C” is the “commit = diff” mistake from the start of the lesson: there is no diff in a commit.
Let’s check. HEAD~1 is HEAD’s parent; git rev-parse turns a name into a full hash:
$ cd ombor && git rev-parse HEAD:README.md HEAD~1:README.md
51c4fee9be20d18260ace91ce44e4b9c0ca396ab
51c4fee9be20d18260ace91ce44e4b9c0ca396abThe README hash is the same in both commits. Now we print every object in the repository with its type (cut -c1-20
keeps the first 20 characters of each line):
$ cd ombor && git cat-file --batch-all-objects --batch-check='%(objecttype) %(objectname)' | cut -c1-20
blob 12b6d3bcf6f52e8
tree 19c7d673a51f53a
blob 51c4fee9be20d18
tree 651f29e7e2b98f1
commit daac3a47cb72f
commit e657f417275efTwo commits, two trees and two blobs, six in total. One blob for README, as in the picture above.
Here you can see the difference from JavaScript. In JS, { a: 1 } and { a: 1 } are two different objects and ===
gives false: an object’s name is its place in memory. In git an object’s name is the hash of its content, so the
same content is always one object. This kind of storage is called content-addressable.
In a cloned or large repository git keeps objects not in separate files but in packs called packfiles (during
git gc, push and clone). Inside a pack, similar files are compressed as differences (deltas). But this is only a
storage method: git cat-file -p still returns the full tree and the full file, and a commit is still a full
snapshot.
Where a hash comes from
Let’s check the hash ourselves. git hash-object --stdin computes the blob hash of text from standard input.
printf 'non: 3000\n' is one line of text (\n is the end of line), 10 bytes in total:
$ printf 'non: 3000\n' | git hash-object --stdin
2ecbbc99c0932c7b1430e07e274c706f3afb62efNow we get the same hash without git. We give sha1sum a header: the word blob, a space and the length (10).
Then comes a zero byte (\0) and the same text:
$ printf 'blob 10\0non: 3000\n' | sha1sum
2ecbbc99c0932c7b1430e07e274c706f3afb62ef -The result matches character for character (- means “read from standard input”). So an object hash =
SHA-1(<type> <length>\0 + content). On macOS use shasum instead of sha1sum.
If we change one digit, does only the end of the hash change?
A) yes, the last one or two characters · B) no, a completely different hash · C) the hash stays the same, the length is the same
Answer
B. Look at the output below: one digit changed, and all 40 characters are different. If you chose “C”, you thought the hash is computed from the length: the length is only part of the header, and the content counts too.
$ printf 'non: 3001\n' | git hash-object --stdin
a3fad3e262bce90907d0538b01444aaa526edd9aA commit is an object too, and its content includes the parent hash. So if you rewrite an old commit, the hashes of
all commits after it change as well: each one holds the hash of the one before it.
SHA-1 is now considered weak. Git’s BreakingChanges document plans to switch the default hash for new repositories to SHA-256 in Git 3.0 (no date is set). Computing the hash ourselves in Go is GIT-2.01.
A branch is a file with one hash, HEAD is the current branch
Now let’s look at the branch itself. First HEAD:
$ cd ombor && cat .git/HEAD
ref: refs/heads/mainThe HEAD file usually holds not a hash but a path to another ref: refs/heads/main. In other words, HEAD says
which branch you are on. And the main file holds:
$ cd ombor && cat .git/refs/heads/main
daac3a47cb72faf965c5385ab1002bc04bf89b45The 40-character hash of the last commit. The Pro Git definition says the same: a branch is a reference to the last commit in a chain of commits. No copy, no folder.
.git/HEAD .git/refs/heads/main .git/objects
┌──────────────────────┐ ┌──────────────────┐ ┌────────────────────┐
│ ref: refs/heads/main │ ──► │ daac3a47cb72…45 │ ─────► │ commit daac3a4 … │
└──────────────────────┘ └──────────────────┘ └────────────────────┘
which branch you're on branch = 41 bytes unchanging objectCompared with JS: a commit is a frozen (Object.freeze) object, and a branch is a let variable that holds a
reference to it. A new commit creates a new object and rebinds the variable to it; the old object stays where it is.
The word “usually” matters. If you switch to a commit directly (git switch --detach), the HEAD file holds a hash:
this is called a detached HEAD. This example is not in the video, it is only for illustration (the final
git switch -q main takes you back to main):
$ cd ombor && git switch -q --detach HEAD~1 && cat .git/HEAD && git switch -q main
e657f417275efcc319ccce4e1badded9976a8fa6A branch by hand (experiment only)
As proof, we create a new branch without the git branch command: we write the hash of the first commit into a plain
file. > writes a command’s output to a file.
$ cd ombor && git rev-parse HEAD~1 > .git/refs/heads/eski && git branch
chegirma
eski
* maineski (“old”) appeared in the git branch list (* marks the branch HEAD is on). A file was written, so the
branch exists. But this is only an experiment for understanding. git branch and git update-ref check the value and
also write to the reflog (the log of where a branch has been, GIT-3.04):
$ cd ombor && ls .git/logs/refs/heads/
chegirma
mainThere is a log for chegirma and main, and none for the hand-written eski. Also:
- git sometimes collects refs into one file,
.git/packed-refs(aftergit gcorgit pack-refs --all); then the files inrefs/headsdisappear; - Git 3.0 plans to store refs in the reftable format for new repositories.
So don’t touch ref files by hand: to create a branch, use git branch <name> <commit>.
Working on a branch: switch, status, add, commit
Now we switch to the chegirma branch and change the price. We wanted a 10 percent discount but made a mistake and
set the price to zero. git switch -q chegirma moves HEAD to chegirma (-q means quiet).
sed -i.bak 's/3000/0/' main.go replaces 3000 with 0 in the file (the .bak backup makes it work on macOS too;
rm deletes it afterwards). git status --short shows changed files in short form:
$ cd ombor && git switch -q chegirma && sed -i.bak 's/3000/0/' main.go && rm main.go.bak && git status --short
M main.go M means the file is modified but not yet added with git add. git add prepares it, then we commit (the time
again comes from vaqt.sh; chegirma: 10 foiz means “discount: 10 percent”):
$ cd ombor && git add main.go && . ../vaqt.sh && git commit -m "chegirma: 10 foiz"
[chegirma 5a239b1] chegirma: 10 foiz
1 file changed, 1 insertion(+), 1 deletion(-)[chegirma 5a239b1] tells you which branch the commit went to and its hash. Now we look at all branches (--all:
from all refs; --decorate: next to each commit, the refs that point to it):
$ cd ombor && git log --oneline --decorate --all
5a239b1 (HEAD -> chegirma) chegirma: 10 foiz
daac3a4 (main) narx dasturi
e657f41 (eski) READMEOnly chegirma moved forward; main stayed where it was. HEAD -> chegirma means the HEAD file now holds
ref: refs/heads/chegirma.
| File | After qur.sh | After the chegirma commit |
|---|---|---|
.git/HEAD | ref: refs/heads/main | ref: refs/heads/chegirma |
refs/heads/main | daac3a4… | daac3a4… |
refs/heads/chegirma | daac3a4… | 5a239b1… |
refs/heads/eski | — | e657f41… |
Merge: fast-forward
Now we go back to main and merge the discount. Will git create a new commit?
A) yes, a merge commit with two parents · B) no, the hash of chegirma is written into the main file · C) the
commit from chegirma is copied into main
Answer
B. Since chegirma was created, main got no new commits: the last commit of main is an ancestor of
chegirma. There is nothing to combine, so git just moves main forward. This is called a fast-forward. “A” is
what happens when both branches have new commits. If you chose “C”, you thought a commit belongs to a branch: there is
one commit, and both branches simply reference it.
$ cd ombor && git switch -q main && git merge chegirma
Updating daac3a4..5a239b1
Fast-forward
main.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)Updating daac3a4..5a239b1: main moved from daac3a4 to 5a239b1. Fast-forward: no merge commit was needed.
Git wrote the hash of chegirma into the main file and also updated the working files (working tree): main.go on
disk is now the version with 0. The two branch files are now the same:
$ cd ombor && cat .git/refs/heads/main .git/refs/heads/chegirma
5a239b10ba4641d54a7500d0de49f8352df729ce
5a239b10ba4641d54a7500d0de49f8352df729ceIf both branches have new commits, a merge commit with two parents appears. We cover it in the second module: GIT-2.03.
Revert: undo a mistake with a new commit
We run the program:
$ cd ombor && go run main.go
non: 0 so'mBread for zero sum. The mistake has already reached the main branch. We don’t delete history; we use git revert:
it writes the opposite change of the given commit as a new commit (--no-edit: automatic message, no editor).
After the revert, how many commits does git log --oneline show?
A) 3: the bad commit leaves the history · B) 4: the bad commit and its reversal · C) 2
Answer
B. Revert deletes nothing, it only adds. If you chose “A”, you confused revert with git reset: reset moves the
branch file back and rewrites history (GIT-3.03).
$ cd ombor && . ../vaqt.sh && git revert --no-edit HEAD
[main 14b041d] Revert "chegirma: 10 foiz"
Date: Mon Jan 5 10:30:00 2026 +0500
1 file changed, 1 insertion(+), 1 deletion(-)The Date: line appears only here because the author time was set separately through vaqt.sh. In your own
repository this line won’t be there.
$ cd ombor && git log --oneline
14b041d Revert "chegirma: 10 foiz"
5a239b1 chegirma: 10 foiz
daac3a4 narx dasturi
e657f41 READMEThe history now has four commits, and none is lost. Let’s check the price:
$ cd ombor && go run main.go
non: 3000 so'mThe price is 3000 sum again. On a branch the whole team works on, this is the safe way: commits your colleagues already have don’t change, a new one is only added on top, so nobody’s history breaks.
But note: revert does not delete the old commit. If a password or key got into a commit, it stays in history, like
5a239b1, and can still be read with git cat-file -p. What to do about it is the next lesson,
GIT-1.02.
The whole model in one table
| Command | What changes inside .git |
|---|---|
git commit | new objects (blobs for changed files, trees, a commit) + a new hash in the current branch file |
git branch X | a new file refs/heads/X with the current commit hash (+ reflog) |
git switch X | ref: refs/heads/X in the HEAD file + the working files match that commit |
git merge X (fast-forward) | the hash of X in the current branch file + the working files |
git revert C | a new commit that is the opposite of C; old objects stay |
In short: a commit is a snapshot of the whole project plus a reference to its parent. A branch is usually a small
file with one hash in it. HEAD shows which branch you are on. A fast-forward merge moves that file forward. Revert
undoes a mistake with a new commit and does not break history. Rebase, reset and reflog work on exactly these
objects and refs.
Common mistakes
Some commands in this section are not in the video: we ran them in the same practice repository and wrote before each one which state it runs from.
1. “A commit is a diff”
The most common wrong picture. If you believe it, you can’t answer “where is an unchanged file stored?” or “why don’t
you need to apply all earlier commits to restore one commit?”. There is no diff in a commit: tree, parent, author,
time and message (section “What is inside a commit”). git show and git diff compute the diff between two
snapshots. Deltas in packs are only a storage method (section “Is an unchanged file stored again?“).
2. “My commit is gone”: git log shows only the current branch
State: after the chegirma commit, before the merge (./qur.sh, git branch chegirma, then the two commands from
“Working on a branch”). If you switch to main and look at the history:
$ cd ombor && git switch -q main && git log --oneline
daac3a4 narx dasturi
e657f41 README5a239b1 is missing. It is not deleted: plain git log walks back from HEAD along parents, and 5a239b1 is not an
ancestor of main. To see all refs, use --all:
$ cd ombor && git log --oneline --all
5a239b1 chegirma: 10 foiz
daac3a4 narx dasturi
e657f41 README3. cd ombor twice
The commands on this page start from the lesson folder. If you are already inside ombor and type cd ombor && …
again:
$ cd ombor && git log --oneline
bash: cd: ombor: No such file or directoryHow to read it: bash: cd: means bash’s cd command wrote the error; ombor: No such file or directory means there
is no file or folder called ombor here, because you are already inside it. The command after && did not run. Fix:
leave out cd ombor && , or run cd .. first. (In zsh the message text is slightly different:
no such file or directory: ombor.)
4. Reading an object with cat
.git/objects/51/c4fee9… is the file of the README blob (the first two characters of the hash are the folder name).
If you open it with cat, unreadable bytes come out. Let’s look at the first two bytes (-tx1 prints each byte in
hexadecimal):
$ cd ombor && head -c 2 .git/objects/51/c4fee9be20d18260ace91ce44e4b9c0ca396ab | od -An -tx1
78 0178 01 is a zlib header (RFC 1950: 78 means deflate compression): the file is compressed. Fix:
git cat-file -p 51c4fee or git cat-file -p HEAD:README.md. In a cloned repository or after git gc this file may
not exist at all: the object is inside a packfile.
5. Writing a ref file by hand, wrongly
State: the end of the lesson (after the revert). We write a branch name instead of a hash:
$ cd ombor && echo chegirma > .git/refs/heads/buzuq && git branch
warning: ignoring broken ref refs/heads/buzuq
chegirma
eski
* mainwarning: ignoring broken ref refs/heads/buzuq means the broken ref refs/heads/buzuq is ignored (buzuq means
“broken”). A ref file is expected to hold a 40-character hash (or ref: …). Git gave no error; the branch is simply
missing from the list. We delete the broken file:
$ cd ombor && rm .git/refs/heads/buzuq && git branch
chegirma
eski
* mainThe right way is a git command: it turns the name into a hash, checks it and writes the reflog.
git branch buzuq chegirma # yoki: git update-ref refs/heads/buzuq chegirma(yoki means “or”.)
6. After git gc the branch file is gone
State: after mistake 5. git gc (-q means quiet) packs objects and refs:
$ cd ombor && git gc -q && cat .git/refs/heads/main
cat: .git/refs/heads/main: No such file or directory“.git/refs/heads/main: no such file or folder”. The branch is not lost; it moved to .git/packed-refs. Wherever a
branch is stored, git rev-parse gives you its hash:
$ cd ombor && git rev-parse main
14b041d603417386d5d82e8f1e0bde53420b4980$ cd ombor && cat .git/packed-refs
# pack-refs with: peeled fully-peeled sorted
5a239b10ba4641d54a7500d0de49f8352df729ce refs/heads/chegirma
e657f417275efcc319ccce4e1badded9976a8fa6 refs/heads/eski
14b041d603417386d5d82e8f1e0bde53420b4980 refs/heads/mainRule: in scripts, write git rev-parse <branch>, not cat .git/refs/heads/….
7. “My hashes differ from the video”
In your own repository (or one built by hand, without qur.sh) the hashes will be different. This is not an error:
the commit content includes the name, the email and the time to the second; other values give another hash. Blob
hashes depend only on the content: printf 'non: 3000\n' | git hash-object --stdin gives 2ecbbc99… everywhere. If
the hashes differ in a repository built with qur.sh, check your git version (git --version, 2.29 or newer).
8. “I reverted, so the key is gone from history”
It isn’t. Revert adds a new commit; the old commit and its blob stay where they are (section “Revert”). A leaked key has to be revoked first: GIT-1.02.
Check yourself
- Which lines does a commit object have, and which one links commits into a chain?
Answer
tree (the project snapshot), parent (the hash of the previous commit; the first commit has none), author,
committer (name, email, Unix time and the offset from UTC) and the message. parent links the chain. There is no
diff line.
- What is in the branch file
.git/refs/heads/main, and why is it 41 bytes?
Answer
The full SHA-1 hash of the last commit: 40 hex characters and the end of line \n, 41 bytes in total. In a SHA-256
repository it would be 65 bytes; after git gc the file may not exist at all (packed-refs).
- Open the commit made by the revert. Which hash is on the
parentline?
cd ombor && git cat-file -p HEADAnswer
The bad commit, 5a239b1…: the revert was added on top of it. And tree is 651f29e…, the same as the second
commit from qur.sh, because the files went back to the 3000-sum state. Same content, same hash.
$ cd ombor && git cat-file -p HEAD
tree 651f29e7e2b98f1f8f5b84a4051ff7e85e41e3fb
parent 5a239b10ba4641d54a7500d0de49f8352df729ce
author Lola <lola@example.com> 1767591000 +0500
committer Lola <lola@example.com> 1767591000 +0500
Revert "chegirma: 10 foiz"
This reverts commit 5a239b10ba4641d54a7500d0de49f8352df729ce.- Why did
git merge chegirmanot create a new commit? When would it have?
Answer
Since chegirma was created, main had no new commits, so git moved the main file to the hash of chegirma
(fast-forward) and updated the working files. When both branches have new commits, a merge commit with two parents
appears (GIT-2.03).
- There is a bad commit on
main, which the whole team uses. Whygit revert, and what does it not do?
Answer
Revert does not rewrite history; it only adds an opposite commit, so your colleagues’ commits and hashes don’t change. But it does not delete the old commit: a password or key that got into it stays in history.
- From SYS-0.01: after
git branch chegirma(in the state right after./qur.sh), what does this print?
cd ombor && find .git/refs -type f | wc -lAnswer
2. find writes two paths (main and chegirma) into the pipe, and wc -l counts them.
$ cd ombor && git branch chegirma && find .git/refs -type f | wc -l
2(The command includes git branch chegirma so you can check it on a clean state after ./qur.sh.)
Homework
Course folder: lessons/git-01-commit-snapshot .
Do everything after the steps from the video, inside ombor.
- 🟢 Easy. Add a new file in
ombor, commit it and open the commit withgit cat-file -p HEAD. Which commit is on theparentline? - 🟡 Medium. Make the discount right, 10 percent:
go run main.go→non: 2700 so'm. - 🟠 Hard.
git branch -d eski: the branch is deleted. What about the commit? - 🔴 Guru. Change the message of the last commit, not yet pushed, with
git commit --amend. Which hashes change and which stay?
The answers script:
homework/javoblar.sh
(bash homework/javoblar.sh, from the lesson folder). It builds ombor from scratch, repeats the steps from the video
and shows each answer. Try it yourself first.
Answer
$ bash homework/javoblar.sh
== Oson: yangi commit va uning ichi
tree 530f9ba8cd2e53324afb489e607065d85c667033
parent 14b041d603417386d5d82e8f1e0bde53420b4980
author Lola <lola@example.com> 1767591600 +0500
committer Lola <lola@example.com> 1767591600 +0500
sut narxi
== O'rta: chegirma to'g'ri, 10 foiz (3000 -> 2700)
non: 2700 so'm
== Qiyin: branch o'chadi, commit qoladi
Deleted branch eski (was e657f41).
e657f417275efcc319ccce4e1badded9976a8fa6 main tarixida bor: commit yo'qolmadi
eski uchun reflog bormi? 0 (qo'lda yozilgan edi)
== Guru: --amend dan oldin va keyin (faqat push qilinmagan commit'da!)
commit: 914b512d66ad3dd5638296a84dde4025e086c339 tree: 4cf5d8f72e7368423bc8301442ffe415074f71e1 parent: 5f4efe002c6d79b8d234ddd4a0cbfdc5ba83f094
commit: cdd8662a7199bdd9232c9abbc859b80325afa472 tree: 4cf5d8f72e7368423bc8301442ffe415074f71e1 parent: 5f4efe002c6d79b8d234ddd4a0cbfdc5ba83f094
eski commit reflog'da: 914b512 commit: chegirma: 10 foiz, to'g'riThe script prints in Uzbek (the headings are the task levels: “Easy”, “Medium”, “Hard”, “Guru”).
🟢 git add + git commit, then git cat-file -p HEAD: parent is the revert commit 14b041d6…, and tree is new
(sut.txt, “milk”, was added).
🟡 sed -i.bak 's/3000/2700/' main.go, git commit -am "…", go run main.go → non: 2700 so'm.
🟠 Deleted branch eski (was e657f41). Only the ref file was deleted. The commit is still there and reachable:
git merge-base --is-ancestor e657f41 main succeeds because it is an ancestor of main. (git cat-file -t is not
enough proof: even a commit no ref reaches still answers commit until gc removes it.) -d deletes only a merged
branch; -D deletes an unmerged one. Then you usually find the commit through git reflog, but the hand-written
eski has no reflog: only git fsck --lost-found helps.
🔴 The commit hash changes (914b512… → cdd8662…), because the message (and, here, the committer time) is part of
the commit content. tree (4cf5d8f7…) and parent (5f4efe00…) stay the same: the files and the earlier history
are unchanged. The old commit is not deleted; it stays in git reflog. Don’t amend a pushed commit: that rewrites
history and causes trouble for colleagues (GIT-2.05, GIT-3.03).
Sources
- Pro Git: 1.3 What is Git? (snapshots, not differences) · 2.3 Viewing the Commit History (author and committer) · 7.8 Advanced Merging (revert or reset)
- Pro Git: 10.2 Git Objects (blob, tree, commit, modes,
zlib, header + SHA-1) · 10.3 Git References (branch,
HEAD,
update-ref) · 10.4 Packfiles - Git manual: git-cat-file , git-hash-object , git-commit (DATE FORMATS) , git-show , git-switch , git-status , git-merge (FAST-FORWARD MERGE) , git-revert , git-update-ref , git-pack-refs
- Git 3.0 BreakingChanges : SHA-256 and reftable plans, no date
- React docs: Updating Objects in State (don’t change state, pass a new object)
- RFC 1950: ZLIB Compressed Data Format (
78is a deflate header) - MIT Missing Semester: Version Control (Git)
- Julia Evans: Inside .git · Do we think of git commits as diffs, snapshots, and/or histories?
- Course folder and README: lessons/git-01-commit-snapshot
- All results were produced with git 2.54.0, GNU bash 5.3.9, GNU coreutils 9.11 and
go1.26.8 linux/amd64on Linux, on 2026-11-26. The notes about macOS (shasum) and Windows (Git Bash) come from the course README and were not tested.
Video and code
▶️ The video is coming soon. This page teaches the whole lesson, so you don’t have to wait for it.
💻 Code and homework answers on GitHub
Next lesson: GIT-1.02 → · All lessons
comments