Lesson 04 beginner

Git Branching

What a branch really is, creating and switching branches with git switch, HEAD and detached HEAD, listing and comparing branches, naming conventions and cleaning up merged branches with the new git branch --delete-merged from Git 2.56.

Branches let you work on several things at once without them getting in each other’s way. The Orbit API can keep running from main while you build a search feature on one branch and a teammate fixes a login bug on another. In Git, branches are so cheap that most people create one for every change, even a one-line fix.

A branch is just a pointer

A branch is a tiny file that holds the ID of one commit. That is all. When you commit on a branch, Git creates the new commit and moves the branch pointer forward to it.

                 main
                  |
  a1f3 <- 5d1e <- 9b7c
                  |
               feature/search

Right after creating feature/search, both names point at the same commit. Commit on the feature branch and only that pointer moves:

                 main
                  |
  a1f3 <- 5d1e <- 9b7c <- c42d
                           |
                     feature/search

HEAD is how Git knows which branch you are on. Normally it points to a branch name, and that branch is the one that moves when you commit.

Create and switch

Since Git 2.23, git switch is the command for moving between branches. You will still see git checkout in older guides; it does the same job plus several unrelated ones, which is why it was split.

$ git switch -c feature/search          # create the branch and switch to it
Switched to a new branch 'feature/search'
$ echo "function search(q) { return []; }" > search.js
$ git add search.js && git commit -m "Add search stub"
$ git switch main                       # go back
$ ls
README.md  server.js                    # search.js is not here: it lives on the branch
$ git switch -                          # jump back to the previous branch
Switched to branch 'feature/search'
Older commandModern equivalent
git checkout feature/searchgit switch feature/search
git checkout -b feature/searchgit switch -c feature/search
git checkout -- server.jsgit restore server.js (lesson 9)

Switching rewrites the files in your working tree to match the target branch. If you have uncommitted changes that would be overwritten, Git refuses to switch. Commit them, stash them (lesson 8) or bring them along with git switch -m.

Listing and inspecting branches

$ git branch
* feature/search
  main
$ git branch -v                       # with the last commit of each
$ git branch -a                       # include remote-tracking branches
$ git branch --merged main            # branches already fully merged into main
$ git branch --no-merged main         # branches with work not in main yet
$ git log --oneline main..feature/search
c42d9e1 Add search stub

main..feature/search reads as “commits on feature/search that are not on main“. It is the quickest way to see what a branch would bring in.

Detached HEAD

If you switch to a commit instead of a branch, HEAD points directly at that commit. This is called detached HEAD:

$ git switch --detach 5d1e0a2
HEAD is now at 5d1e0a2 Add README and API entry point

It is perfect for looking around: run the old version, test something, read files. If you commit here, though, no branch points at the new commits, and once you switch away they are hard to find. If you want to keep work done in detached HEAD, give it a name before leaving: git switch -c experiment.

Renaming and deleting

$ git branch -m feature/search feature/full-text-search   # rename
$ git branch -d feature/full-text-search                  # delete if merged
error: the branch 'feature/full-text-search' is not fully merged.
$ git branch -D feature/full-text-search                  # force delete

-d protects you: it refuses to delete a branch whose commits exist nowhere else. -D deletes anyway. Even then, the commits are recoverable for a while through the reflog (lesson 9).

Cleaning up merged branches (Git 2.56)

After a few weeks, a repository collects dozens of local branches that were merged long ago. Git 2.56 added a built-in way to clean them up, based on each branch’s upstream:

$ git branch --delete-merged 'origin/*' --dry-run     # list what would go
$ git branch --delete-merged 'origin/*'               # delete them

The pattern 'origin/*' is matched against the branch’s upstream (lesson 7). A branch is deleted only when its tip is already contained in that upstream. You can add a second pattern to restrict which local names are candidates, for example 'feature/*'. Branches checked out in a worktree and branches whose upstream is gone are skipped. To protect one branch from cleanup forever:

$ git config branch.release/2026.10.deleteMerged false

Tip: Always run the --dry-run first. On an older Git, the classic equivalent is git branch --merged main to list candidates and git branch -d on each name.

Naming branches

Git accepts almost any name, but teams usually agree on a pattern so branches sort and filter nicely:

PrefixUseExample
feature/New functionalityfeature/search
fix/Bug fixfix/login-timeout
chore/Maintenance, dependencieschore/update-node-24
docs/Documentation onlydocs/deploy-guide
release/Release preparationrelease/2026.11

Add the ticket number when your team tracks work in issues: fix/ORB-142-login-timeout. Keep names short, lowercase and without spaces.

Quick reference

TaskCommand
List branchesgit branch / git branch -a
Create and switchgit switch -c <name>
Switchgit switch <name>
Previous branchgit switch -
Commits on a branch but not on maingit log main..<branch>
Renamegit branch -m <old> <new>
Delete merged / force deletegit branch -d / git branch -D
Clean up merged branches (2.56)git branch --delete-merged 'origin/*' --dry-run

Exercise

You create feature/search from main and make two commits on it. What happens to the main branch pointer?

  1. It moves forward too, because both branches share history
  2. It is deleted until the branches are merged
  3. It moves to the first of the two new commits
  4. It stays where it was; only feature/search moves forward
Show answer

Answer: D. A branch is a pointer to one commit. Only the branch HEAD points to moves when you commit. main keeps pointing at the commit it had until something is merged into it.

Exercise

You checked out an old commit to test it, made a quick fix and committed. Now you want to keep that fix. What should you do before switching away?

  1. Nothing, the commit is saved on main automatically
  2. Run git stash to keep the commit
  3. Create a branch at that point with git switch -c and the new branch name
  4. Run git branch -D to protect the commit
Show answer

Answer: C. In detached HEAD no branch points at your new commit. Creating a branch there gives it a name, so it stays easy to find after you switch.

Exercise

Your machine has 40 old local branches. You want to delete only those already merged into their upstream on origin, but first see the list. Which command?

  1. git branch -D –all
  2. git branch –delete-merged ‘origin/*’ –dry-run
  3. git clean -fd –branches
  4. git remote prune origin –dry-run
Show answer

Answer: B. git branch –delete-merged, new in Git 2.56, deletes local branches whose tips are already contained in their matching upstream. –dry-run lists them without deleting anything. git remote prune only removes stale remote-tracking references.

Key takeaways

  • A branch is a pointer to a commit; committing moves the branch HEAD points to.
  • Use git switch and git switch -c to move between and create branches.
  • git log main..branch shows what a branch would add to main.
  • Detached HEAD is for looking around; create a branch before leaving if you want to keep commits.
  • git branch -d refuses to lose work, and Git 2.56’s --delete-merged cleans up merged branches in bulk.

FAQ

Do branches copy my files and use a lot of disk space?

No. A branch is a reference of a few bytes pointing to a commit. Creating a hundred branches costs nothing, which is why Git encourages one branch per change.

Should I use git switch or git checkout?

Use git switch for branches and git restore for files. They were declared stable in Git 2.51 and their names make mistakes less likely. git checkout still works and you will see it in scripts and older documentation.

Why can I not switch branches? Git says my changes would be overwritten.

You have uncommitted changes in files that differ on the target branch. Commit them, stash them with git stash, or carry them along with git switch -m <branch>.

How long should a branch live?

As short as possible: hours or a few days. Long-lived branches drift away from main and end in painful merges. Lesson 14 explains trunk-based development, where this habit is a rule.

I deleted a branch by mistake. Can I get it back?

Usually yes. Git printed the commit ID when it deleted the branch (Deleted branch x (was c42d9e1)). Run git branch x c42d9e1. If you lost the ID, git reflog lists recent positions of HEAD.

↑ ↓ navigate Enter open Esc close