Skip to content

Branch Concept

A branch lets us give a line of work a name and advance it without moving another line. The problem it solves is simple: a team needs to develop a feature while the main line can continue independently.

A local branch is a named reference to a Commit. Creating feature at Commit C3 initially gives us two names for the same Commit:

master → C3 → C2
feature → C3 → C2

Creating that branch does not create a Commit or duplicate the project’s files, Trees, Blobs, or history. Git only needs another reference. This makes branch creation lightweight and fast even in a large repository.

A branch can begin at a chosen existing Commit, not only at the Commit reached by the current branch. The new reference initially holds the chosen Commit ID. The commands for creating and selecting branches belong to the next lesson.

When the current branch is feature and we make a new Commit C4, Git records C3 as its parent and moves only feature:

feature → C4 → C3 ← master

If work then continues on master and creates C5, the history diverges:

feature → C4 → C3 ← C5 ← master

C4 and C5 both have C3 as their parent. Neither branch is a separate repository: both refs and all their objects live in the same repository. master does not point to C4, and following parent links from its tip does not reach C4.

Suppose C4 changes app/User.php. Its snapshot can reference a new Blob and new Trees along that file’s path. The snapshot recorded by C3 still references its original objects. Moving feature to C4 does not edit C3 or the value of master.

In a Backend project, this lets a team work on a registration fix on one branch while keeping a separate line for other changes. The branches may share almost all their unchanged objects; the branch names simply identify different Commit tips.

  • Thinking a branch is a physical copy of the whole repository.
  • Thinking that creating a branch automatically creates a Commit.
  • Expecting a Commit on feature to move master too.
  • Thinking a newer Commit rewrites the snapshot of its parent.
  • Thinking objects on another branch are stored in another repository.

Branch creation adds a named reference to an existing Commit. A new Commit moves the current branch reference and points back to its parent. Other branches keep their values until an operation explicitly moves them. The shared objects and immutable history make branching fast.

Both branches point to C3. A new Commit C4 is created on feature. Where do the refs point?

feature moves to C4; master stays at C3. C4 has C3 as its parent.

Why does creating a branch not require copying thousands of files?

The new branch is a reference to an existing Commit ID. That Commit already connects to the snapshot’s Trees and Blobs.