Part IV: Chapter 4.2

git branch

Creating, Listing, Deleting

The git branch command is essentially a file manager for .git/refs/heads/. Creating a branch writes a new 41-byte file. Deleting one removes it. Listing them reads the directory. Nothing more.

Live Branch Manager

Run commands to create and list branches. Watch the ref files appear and disappear in real time. Click the trash icon or type a branch name and hit delete.

git branch

.git/refs/heads/

mainc4d5e6f...41 B

Terminal

Run a command above...

Listing

git branch with no arguments reads every file in .git/refs/heads/ and prints their names. The asterisk (*) marks the branch currently pointed to by HEAD.

Add -v to also print the hash and latest commit message for each branch.

Creating

git branch <name> creates a new file at .git/refs/heads/<name> containing the hash of the current HEADcommit. The new branch points to the same commit you're on right now.

Note: this does not switch to the new branch. For that, use git checkout -b <name> or git switch -c <name>.

Deleting

git branch -d <name> deletes the ref file. Git will refuse if the branch has commits not reachable from any other branch — use -D to force.

Deleting a branch never deletes commits. The commit objects stay in .git/objects/ until git gc cleans up unreachable objects.

Common Patterns

The most common branch operations, including the shorthand that creates and switches in one step.

Terminal
$git branch
* main
develop
$git branch -v
* main c4d5e6f Fix nav spacing
develop a1b2c3d Add homepage
$# Create a branch:
$git branch feature
$# Create AND switch (the common shorthand):
$git switch -c hotfix
Switched to a new branch 'hotfix'
$# Delete a merged branch:
$git branch -d feature
Deleted branch feature (was c4d5e6f).