Git (Distributed Version Control System)

Git is a distributed version control tool that organizes version history through content-addressable objects and references. A standard full clone allows committing and inspecting history locally; a branch is a movable reference pointing to a commit, and HEAD normally points to the current branch or directly to a commit.

Second-Draft Verification Note (2026-10-15): This article is the second draft of the HeLuoXu tutorial, revised against official specifications and documentation. Earlier experiment logs are labeled separately as "Draft 1 Retained Output" and do not represent a re-run on Windows in this batch; see the batch verification record for the current environment and results. The "StarRiver Notes" service, accounts, orders, prices, and policies in the examples are fictional teaching data.

One-Sentence Definition

Git is a distributed version control tool that organizes version history using content-addressable objects and references. A standard full clone supports local commits and history inspection; a branch is a movable reference pointing to a commit, while HEAD usually points to the current branch or directly to a commit.

Why It Matters

  • De facto industry standard: GitHub, GitLab, and model and dataset repositories on Hugging Face Hub all use Git underneath. Hugging Face Hub repository documentation: https://huggingface.co/docs/hub/repositories.
  • Once you understand Git's object model, intimidating operations like reset, rebase, and detached HEAD become predictable, and many committed mistakes can be located and recovered through an unexpired reflog.
  • AI coding assistants and CI/CD pipelines (k6.swe.ci-cd) both rely on Git commits as the basic unit for triggers and rollback.

How It Works / Diagram

Three areas: Working tree (the files you see) → Staging area (index, the draft of the next commit) → Repository (.git/objects).

Four object types — Pro Git 10.2 Git Objects: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects

Object What It Stores Analogy
blob File contents (without filename) A sheet of paper
tree List mapping filenames → blobs / sub-trees Directory
commit Root tree + parent commit(s) + author + timestamp + message A labeled snapshot
tag An annotated tag pointing to an object Bookmark

In a SHA-1 repository, an object's ID is sha1("<type> <byte-length>\0" + content). Because the parent commit hash is embedded inside the commit content, rewriting a commit and rebuilding its descendant chain generates new descendant IDs; the original objects themselves are never modified in place — which is why rewriting already-pushed history causes trouble for collaborators.

Figure 1

Git is advancing its SHA-256 object format (git init --object-format=sha256); see https://git-scm.com/docs/hash-function-transition.

Scope Boundaries & Verification Notes

The SHA-1 formula applies to the SHA-1 object format; not every repository is fixed to SHA-1. References can be stored in loose files, packed-refs, or other reference backends, so "a branch is just a text file" is specifically an on-disk observation of this example. The reflog is a local record of reference movements that expires over time, and unreachable objects can be garbage-collected; it is not a backup and does not guarantee recovery of working tree changes. Staged but uncommitted data can sometimes be recovered from dangling objects, without any reliable retention guarantee. Fixing timestamps alone is not enough to reproduce identical commit IDs across machines; byte contents, parent objects, signatures, object format, and related configurations must also be identical.

Primary references verified in this review (2026-10-15):

Runnable Example

The following Bash script demonstrates Git objects, the staging area, references, and recovery operations in a temporary directory. The Draft 1 log lists Git 2.47.3; see the re-run log for the system and tool versions of the current batch.

#!/usr/bin/env bash
# File: examples/02_git.sh
# Git internals experiment: objects, references, staging area, and recovery via reflog. Pin author and dates for reproducible commit hashes.
set -euo pipefail
export GIT_AUTHOR_NAME=xiaoming GIT_AUTHOR_EMAIL=xm@example.com GIT_COMMITTER_NAME=xiaoming GIT_COMMITTER_EMAIL=xm@example.com
export GIT_AUTHOR_DATE="2026-10-14T10:00:00+08:00" GIT_COMMITTER_DATE="2026-10-14T10:00:00+08:00"
D=$(mktemp -d); cd "$D"
git init -q -b main demo && cd demo
git --version

echo "== (1) File content = one blob object"
printf 'You are StarRiver Notes customer support.\n' > prompt.txt
git hash-object prompt.txt
"${PY:-python3}" -c "
import hashlib; data=open('prompt.txt','rb').read()
print(hashlib.sha1(b'blob %d\x00' % len(data) + data).hexdigest(), '<- manual sha1(\"blob length\\\\0\"+content)')"

echo "== (2) Working tree -> Staging area -> Commit"
git status --short; git add prompt.txt; git status --short
git commit -q -m "Initial prompt"; git log --oneline
echo "-- Commit object content:"; git cat-file -p HEAD
echo "-- Tree object content:"; git cat-file -p 'HEAD^{tree}'
echo "-- A branch is a text file holding a commit hash:"; cat .git/refs/heads/main; cat .git/HEAD

echo "== (3) Commit twice more, inspect history and diffs"
printf 'Keep answers concise.\n' >> prompt.txt; git commit -q -am "Require conciseness"
printf 'Say you do not know when unsure.\n' >> prompt.txt; git commit -q -am "Add abstention rule"
git log --oneline; git diff HEAD~2 HEAD --stat; git diff HEAD~1 HEAD | tail -3
echo "-- Total object count:"; git count-objects -v | grep '^count'

echo "== (4) Mistake recovery: reset --hard drops the last two commits, then recover via reflog"
git reset -q --hard HEAD~2; git log --oneline
git reflog -4
git reset -q --hard 'HEAD@{1}'; git log --oneline -1

echo "== (5) Trace which commit introduced each line: git blame"
git blame --date=short prompt.txt | cut -c1-60

echo "== (6) .gitignore prevents committing secrets and large binaries"
printf 'OPENAI_API_KEY=sk-fake-000\n' > .env; head -c 1000000 /dev/zero > model.bin
printf '.env\n*.bin\n' > .gitignore; git status --short --ignored
rm -rf "$D"

Draft 1 Retained Output

The log below is preserved from Draft 1 and comes from the original environment reported in Draft 1; it is not a re-run result from this batch. Code changes in Draft 2 may produce different outputs; refer to the batch verification record for actual re-run status.

git version 2.47.3
== (1) File content = one blob object
ac08f29a06f24e207143087e7fbabc64f4315750
ac08f29a06f24e207143087e7fbabc64f4315750 <- manual sha1("blob length\0"+content)
== (2) Working tree -> Staging area -> Commit
?? prompt.txt
A  prompt.txt
a23d31b Initial prompt
-- Commit object content:
tree 40a6c5413a245819e63bbd75507e70ab38d8ee2c
author xiaoming <xm@example.com> 1791943200 +0800
committer xiaoming <xm@example.com> 1791943200 +0800

Initial prompt
-- Tree object content:
100644 blob ac08f29a06f24e207143087e7fbabc64f4315750	prompt.txt
-- A branch is a text file holding a commit hash:
a23d31b5549166325f79a30a223ab6dcbed42135
ref: refs/heads/main
== (3) Commit twice more, inspect history and diffs
18d1151 Add abstention rule
bb3f2d2 Require conciseness
a23d31b Initial prompt
 prompt.txt | 2 ++
 1 file changed, 2 insertions(+)
 You are StarRiver Notes customer support.
 Keep answers concise.
+Say you do not know when unsure.
-- Total object count:
count: 9
== (4) Mistake recovery: reset --hard drops the last two commits, then recover via reflog
a23d31b Initial prompt
a23d31b HEAD@{0}: reset: moving to HEAD~2
18d1151 HEAD@{1}: commit: Add abstention rule
bb3f2d2 HEAD@{2}: commit: Require conciseness
a23d31b HEAD@{3}: commit (initial): Initial prompt
18d1151 Add abstention rule
== (5) Trace which commit introduced each line: git blame
^a23d31b (xiaoming 2026-10-14 1) You are StarRiver Notes customer support.
bb3f2d27 (xiaoming 2026-10-14 2) Keep answers concise.
18d11510 (xiaoming 2026-10-14 3) Say you do not know when unsure.
== (6) .gitignore prevents committing secrets and large binaries
?? .gitignore
!! .env
!! model.bin
  • (1) The hash ac08f29a... returned by git hash-object matches the manual Python calculation sha1("blob length\0"+content) exactly — a blob's name is the fingerprint of its byte content, independent of the filename.
  • (2) ?? indicates an untracked file, which becomes A (staged) after git add. A commit object contains only tree, author, committer, and the commit message (the initial commit has no parent line); 1791943200 is the Unix timestamp for 2026-10-14 10:00 +08:00. .git/refs/heads/main holds a single 40-character hash line, and HEAD contains ref: refs/heads/main — what is observed here is a loose reference file; logically a branch is a reference and is not tied to this specific on-disk storage format.
  • (3) After 3 commits, the object count is 9: 3 commits + 3 trees + 3 blobs (because prompt.txt changed each time).
  • (4) After reset --hard HEAD~2, the reachable history of the current branch returns to a23d31b, but reflog still records HEAD@{1} as 18d1151, allowing recovery in this local demonstration. The default expiration threshold for unreachable reflog entries is governed by gc.reflogExpireUnreachable (typically 30 days), which is a configurable cleanup policy rather than a guaranteed object recovery service: https://git-scm.com/docs/git-gc.
  • (5) git blame identifies which commit introduced each line; the ^ prefix on ^a23d31b denotes the boundary (initial) commit.
  • (6) Once .gitignore takes effect, .env and model.bin appear as !! (ignored) and will not be swept into the repository by git add ..
  • Draft 1 scripting note: with set -o pipefail enabled, an earlier command git log --oneline | head -1 caused the script to abort with exit code 141 — head closed the pipe after reading one line, sending SIGPIPE to git. Switching to git log -1 resolved this (see k6.ops.shell-script).

Self-Test

Question: If you add an already-committed .env file to .gitignore, will the secret disappear from commit history?

Reference Answer: No. .gitignore primarily governs untracked paths; a leaked secret should first be revoked or rotated immediately, followed by cleaning the repository history and other copies according to team collaboration procedures.

Common Pitfalls

  • "Changes made after git add will also be included in the commit": The staging area captures a snapshot at the moment git add runs; subsequent edits require running git add again.
  • ".gitignore makes already-committed files disappear": It only affects untracked files; tracked files must be removed from the index with git rm --cached, and if a secret was committed it must be revoked immediately because it still remains in history (see k6.sec.secret-management).
  • "Everything is lost after reset --hard": Previously committed content can usually be recovered via reflog; unsaved working tree edits generally cannot be recovered by Git, and staged or committed objects remain subject to retention and garbage-collection conditions.
  • "Commit hashes are random": A commit hash is determined by the content, parent commit(s), author, and timestamps; fixing these values makes the hash reproducible, as shown in the example.

Relevance to AI Development

  • Before letting an AI coding assistant execute Git commands, review whether they contain push --force, reset --hard, or clean -fdx, which can overwrite your working tree or rewrite shared history (see the high-risk Bash command table in k6.ops.shell-script).
  • Do not commit model weights or datasets directly into Git; Hugging Face Hub uses Git + LFS/Xet underneath for large files.
  • Use git bisect to binary-search which commit caused an evaluation score regression; paired with an automated evaluation script, it can isolate the culprit commit automatically: https://git-scm.com/docs/git-bisect.

Further Reading

Unverified Scope

  • Hosting platform compatibility with SHA-256 repositories, alternative reference backends, and history cleanup configurations was not verified. See the batch verification record for Bash script execution status.

Example Execution Scope for This Batch

2026-10-15: Not re-run. Revised through documentation and code review against the listed references; the full script was not executed in this batch. Hosting platform compatibility with SHA-256 repositories, alternative reference backends, and history cleanup configurations was not verified. See the batch verification record for Bash script execution status.

"Draft 1 Retained Output" preserves the original log text and is not newly generated by the Draft 2 code; its original conclusions should be read alongside the Draft 2 revisions and scope boundaries.

Back to Foundations Back to Learn →Back to top ↑
On this page
Tutorial diagram100%
Tutorial diagram