Git Branching Mechanics: Pointers, HEAD, and git branch
Git Branching Mechanics: Pointers, HEAD, and git branch
Branching is Git's superpower. While older version control systems treated branches as slow, expensive filesystem copies, Git implements branches as ultra-lightweight, movable 41-byte pointers to commits. Creating, switching, and deleting branches in Git takes less than a millisecond.
1. What is a Git Branch Under the Hood?
In Git, a commit is a node in a Directed Acyclic Graph (DAG) pointing back to its parent commit.
A branch is simply an ordinary text file in .git/refs/heads/<branch-name> containing the 40-character SHA-1 hash of the most recent commit on that line of development:
When you make a new commit on main, Git does not copy your project:
- 1It creates the new commit object
C4whose parent isC3. - 2It automatically updates the
[main]pointer file to point toC4!
2. The Role of the HEAD Pointer
If you have multiple branches (main, feature-login, hotfix), how does Git know which branch you are currently working on? Git maintains a special pointer called HEAD:
HEADis a pointer to the current local branch you have checked out.- Under the hood, the file
.git/HEADcontains:ref: refs/heads/main.
When you create a new branch called dev, Git simply creates a second pointer pointing to the exact same commit:
3. Working with git branch
A. List Local Branches
The asterisk * and green highlight indicates the active branch pointed to by HEAD:
B. List Remote and Local Branches
C. Create a New Branch (Without Switching)
D. Delete a Branch Safely
Once a feature branch is merged into main, clean it up:
E. Force Delete an Unmerged Branch
If you abandoned a spike or prototype that was never merged:
F. Rename the Current Branch
4. Why Branching Transforms Team Engineering
- Zero-Risk Isolation: You can experiment, break things, and try wild architectural changes without risking production stability.
- Concurrent Feature Development: 50 engineers can develop 50 separate features simultaneously on isolated branches without stepping on each other's code.
- Short-Lived Feature Branches: Industry standard engineering creates a branch for a single ticket, completes it in 1–2 days, opens a Pull Request, merges it, and deletes the branch.
Practice Quiz
.git directory?HEAD refer to?feature-cart without automatically switching to it?git branch -d and git branch -D?git branch output, what indicates the currently checked-out branch?Switching Branches with git switch and git checkout -b
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.