0
0
Gitdevops~10 mins

How branches are just files with hashes in Git - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - How branches are just files with hashes
Create branch file
Write commit hash inside
Git reads branch file
Uses hash to find commit
Checkout or update branch
Branch points to commit hash
Git stores each branch as a simple file containing the commit hash it points to. Git reads this file to know the current commit of the branch.
Execution Sample
Git
echo abc123 > .git/refs/heads/main
cat .git/refs/heads/main
Create a branch file 'main' with commit hash 'abc123' and read its content.
Process Table
StepActionFile AffectedContent Written/ReadResult
1Create branch file.git/refs/heads/mainabc123Branch 'main' points to commit abc123
2Read branch file.git/refs/heads/mainabc123Git knows 'main' points to commit abc123
3Checkout branchN/AN/AGit uses hash abc123 to set HEAD and working directory
4Update branch.git/refs/heads/maindef456Branch 'main' now points to commit def456
5Read updated branch file.git/refs/heads/maindef456Git knows 'main' points to commit def456
💡 Execution stops after branch file content is read and used to point to a commit hash.
Status Tracker
VariableStartAfter Step 1After Step 4Final
branch_file_contentemptyabc123def456def456
Key Moments - 3 Insights
Why is the branch just a file with a hash inside?
Because Git uses simple files to store the commit hash that the branch points to, as shown in steps 1 and 2 of the execution table.
What happens when the branch file content changes?
Git updates the branch pointer to a new commit hash, as seen in step 4 where the content changes from 'abc123' to 'def456'.
How does Git know which commit a branch points to?
Git reads the branch file content (the commit hash) to find the commit, demonstrated in steps 2 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of the branch file after step 1?
Adef456
Bempty
Cabc123
DHEAD
💡 Hint
Check the 'Content Written/Read' column at step 1 in the execution table.
At which step does the branch file content change to point to a new commit?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where the 'Content Written/Read' changes from 'abc123' to 'def456'.
If the branch file was empty, what would Git know about the branch?
AIt points to the latest commit
BIt points to no commit
CIt points to the initial commit
DIt points to HEAD
💡 Hint
Refer to the 'branch_file_content' variable in the variable tracker and what an empty value means.
Concept Snapshot
Branches in Git are simple files stored in .git/refs/heads/ directory.
Each branch file contains a commit hash as plain text.
Git reads this hash to know which commit the branch points to.
Updating the branch means changing the hash inside the file.
This simple file-hash system makes branches lightweight and fast.
Full Transcript
In Git, branches are just files that store commit hashes. When you create a branch, Git makes a file named after the branch inside the .git/refs/heads/ folder. This file contains the hash of the commit the branch points to. When Git needs to know the current commit of a branch, it reads this file. Changing the branch means updating the hash inside the file. This simple mechanism allows Git to track branches efficiently.