Core Git 3/8

Branches

A branch is a movable label attached to a commit. Creating a branch adds another label to the history, and each new commit moves that branch forward. Branches let teams isolate unfinished work and create separate lines of development without copying the repository.

Back to Git Concepts overview

A Branch Is A Pointer (Label) To One Commit

Technically, a branch is just a named reference that stores which commit it currently points to.

feature-login a1b2c3d
git branch feature-login

Branch Labels Sit On Top Of Commit History

The branch label does not replace the history graph. It simply names one commit inside that graph. The commits themselves do not know branches exist, because branches do not contain commits; they only point to them.

a1b2c3d b2c3d4e c3d4e5f e5f6g7h d4e5f6g feature-login main a1b2c3d b2c3d4e c3d4e5f e5f6g7h f6g7h8i d4e5f6g feature-login main New commit: the branch label slides forward to the newer commit.
git log --oneline --graph --decorate
git commit -m "feat: continue main work"

Creating A Branch Adds A Label, Not A Commit

Creating a branch does not create a new commit. Git simply adds another label, so main and feature/ui can both point to the exact same commit until one of them moves forward. Multiple branches can point to the same commit at the same time.

COMMAND RUN > git branch feature/ui a1b2c3d b2c3d4e c3d4e5f main feature/ui Both labels point to the same commit immediately after the branch is created.
git branch feature/ui
git log --oneline --decorate

A New Branch Starts A New Line Of Work

After a branch is created, both branches still share the same history up to the split point. From there, each branch can receive new commits independently without affecting the other.

a1b2 b2c3 c3d4 d4e5 e5f6 f6g7 g7h8 h8i9 i9j0 feature/ui main shared history up to the split point after that, main and feature/ui move independently
git switch -c feature/ui
git commit -m "feat: update UI"
Previous DAG + History Graph All Concepts Back To Overview Next HEAD