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.
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:
C4 whose parent is C3.[main] pointer file to point to C4!HEAD PointerIf 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:
HEAD is a pointer to the current local branch you have checked out..git/HEAD contains: ref: refs/heads/main.When you create a new branch called dev, Git simply creates a second pointer pointing to the exact same commit:
git branchThe asterisk * and green highlight indicates the active branch pointed to by HEAD:
Once a feature branch is merged into main, clean it up:
If you abandoned a spike or prototype that was never merged:
.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?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.