Bird
Raised Fist0
Gitdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does a Git branch file store internally?
easy
A. A commit hash pointing to a specific commit
B. The entire project files
C. The list of all branches
D. The commit message text

Solution

  1. Step 1: Understand what a branch represents

    A branch in Git is a pointer to a commit, not the files themselves.
  2. Step 2: Identify what is stored in the branch file

    The branch file stores the commit hash it points to, which identifies the commit.
  3. Final Answer:

    A commit hash pointing to a specific commit -> Option A
  4. Quick Check:

    Branch file = commit hash [OK]
Hint: Branches store commit hashes, not files or messages [OK]
Common Mistakes:
  • Thinking branches store full project files
  • Confusing branch files with commit messages
  • Assuming branches list all other branches
2. Which command shows the content of the branch file for main in Git?
easy
A. git show main
B. cat .git/refs/heads/main
C. git branch main
D. git log main

Solution

  1. Step 1: Locate branch files in Git directory

    Branch files are stored in .git/refs/heads/ folder.
  2. Step 2: Use a command to read the branch file content

    The cat command reads the file content, showing the commit hash.
  3. Final Answer:

    cat .git/refs/heads/main -> Option B
  4. Quick Check:

    Read branch file with cat [OK]
Hint: Branch files are plain files under .git/refs/heads/ [OK]
Common Mistakes:
  • Using git show which shows commit details, not raw hash
  • Using git branch which lists branches, not file content
  • Using git log which shows commit history, not branch file
3. Given the branch file .git/refs/heads/feature contains abc1234, what does git checkout feature do?
medium
A. Creates a new commit with hash abc1234
B. Deletes the feature branch
C. Moves HEAD to commit abc1234 and updates working files
D. Shows the commit message of abc1234

Solution

  1. Step 1: Understand what git checkout does with a branch

    It moves the HEAD pointer to the commit the branch points to.
  2. Step 2: Connect branch file content to checkout effect

    The branch file has commit hash abc1234, so checkout moves HEAD there and updates files.
  3. Final Answer:

    Moves HEAD to commit abc1234 and updates working files -> Option C
  4. Quick Check:

    Checkout moves HEAD to branch commit [OK]
Hint: Checkout moves HEAD to branch's commit hash [OK]
Common Mistakes:
  • Thinking checkout deletes branches
  • Confusing checkout with commit creation
  • Assuming checkout only shows commit messages
4. You see that .git/refs/heads/dev file is empty. What problem will this cause?
medium
A. Git will ignore the dev branch and use main instead
B. Git will create a new commit automatically
C. Git will treat dev as a remote branch
D. Git cannot find the commit for branch dev, causing errors on checkout

Solution

  1. Step 1: Understand branch file role in Git

    The branch file must contain a commit hash to point to a commit.
  2. Step 2: Analyze effect of empty branch file

    If empty, Git cannot find the commit, so checkout or other commands fail with errors.
  3. Final Answer:

    Git cannot find the commit for branch dev, causing errors on checkout -> Option D
  4. Quick Check:

    Empty branch file breaks commit reference [OK]
Hint: Branch files must have commit hashes, never be empty [OK]
Common Mistakes:
  • Assuming Git auto-fixes empty branch files
  • Confusing empty branch file with remote branch
  • Thinking Git defaults to main silently
5. You want to create a new branch release pointing to the same commit as main. Which command correctly updates the branch file?
hard
A. git checkout -b release main
B. git branch release main
C. A and B
D. echo $(cat .git/refs/heads/main) > .git/refs/heads/release

Solution

  1. Step 1: Understand how branches point to commits

    Branches are files storing commit hashes. Copying main's hash creates release at same commit.
  2. Step 2: Analyze each option's effect

    echo $(cat .git/refs/heads/main) > .git/refs/heads/release manually copies the hash file content. git branch release main creates branch release at main's commit. git checkout -b release main creates and switches to release at main's commit.
  3. Step 3: Confirm which options update branch file correctly

    Options A, B, and D all create the release branch pointing to main's commit, but only D is a single command that creates and switches to the branch.
  4. Final Answer:

    git checkout -b release main -> Option A
  5. Quick Check:

    git checkout -b creates and switches branch [OK]
Hint: Multiple ways to set branch file to same commit hash, but checkout -b is most common [OK]
Common Mistakes:
  • Thinking only git commands can create branches
  • Ignoring manual file editing possibility
  • Confusing checkout with branch creation only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard