Great work!

XP to next level

BugEater

Listing, Naming and Deleting Branches

Learning Objectives

By the end of this lesson you will be able to:

  • List local and remote branches, and see which ones are already merged
  • Name a branch so a colleague understands it without asking
  • Delete a merged branch safely with git branch -d
  • Understand precisely what -D overrides and when that is acceptable

Seeing What Exists

git branch
  main
* feature/auth-test
  test/PAY-4417-verify
  old-tests

Local branches only, with * marking where you are.

git branch -a

Adds remote-tracking branches — the ones prefixed remotes/origin/. This is your picture of what is on the server, and it is only as fresh as your last git fetch. Module 6 covers that properly.

git branch -v
  main               8f3c2a1 Add password reset endpoint
* feature/auth-test  d4e5f6a Add negative cases for expired token
  old-tests          1a2b3c4 Initial smoke suite

The last commit on each branch — often enough to remember what a branch was for.

The Two Filters Worth Memorising

git branch --merged

Branches whose work is already contained in your current branch. These are safe to delete — nothing is lost, because their commits live on somewhere else too.

git branch --no-merged

Branches holding commits that exist nowhere else. Deleting one of these genuinely loses work.

Together they turn branch cleanup from a guess into a decision.

Naming Branches Well

A branch name is read far more often than it is typed. Three properties make one good:

A type prefix. feature/, fix/, hotfix/, test/, release/. It tells the reader what kind of thing this is before anything else.

The ticket ID, if there is one. test/PAY-4417-expired-token connects the branch to the tracker automatically, and many tools will link them for you.

A human-readable summary, in lowercase with hyphens. Not test/fix — you will have four of those by Friday.

Poor Better
mybranch test/PAY-4417-expired-token
fix fix/login-timeout-on-slow-network
Johns_Test_Branch_FINAL_2 qa/checkout-regression-suite

Avoid spaces, avoid uppercase (some filesystems treat Test and test as the same name and some don't — this causes real, confusing breakage), and keep it under about 50 characters.

Renaming is easy if you get it wrong:

git branch -m old-name new-name          # rename any branch
git branch -m new-name                   # rename the current one

Deleting a Branch

git branch -d old-tests
Deleted branch old-tests (was 1a2b3c4).

The lowercase -d is the safe delete. Git checks first: if the branch contains commits that are not reachable from anywhere else, it refuses:

error: The branch 'test/experiment' is not fully merged.
If you are sure you want to delete it, run 'git branch -D test/experiment'.

That is not an obstacle to work around. It is Git telling you the branch holds unique work, which is exactly the information you needed.

You also cannot delete the branch you are standing on. Switch away first — usually git switch main.

-D: the Override

git branch -D test/experiment

Uppercase -D deletes regardless. Use it when you know the branch holds only work you want gone — a failed experiment, a branch you already merged elsewhere and verified.

The commits are not instantly destroyed; git reflog can usually still find them for a while. But "usually" and "for a while" are not a backup strategy. Before -D on anything you're unsure about, look:

git log main..test/experiment --oneline

That lists exactly the commits that exist on test/experiment and nowhere in main. If the output is empty, -d would have worked anyway and you don't need -D. If it isn't empty, read it before deciding.

Pro Tip: Clean up your merged branches once a week. git branch --merged gives you the list, and a repository with six meaningful branches is far easier to navigate than one with sixty stale ones.

Deleting a Remote Branch

Deleting locally changes nothing on the server. That takes a separate, explicit command:

git push origin --delete test/PAY-4417-expired-token

Treat this one with more care than the local delete — it affects everyone. Most teams delete the remote branch automatically when a Pull Request merges, so you rarely need to do it by hand.

Key Takeaways

  • git branch lists local branches; -a adds remotes; -v shows each branch's last commit
  • git branch --merged is the safe-to-delete list; --no-merged holds unique work
  • Name branches type/TICKET-short-summary, lowercase, hyphenated, no spaces
  • git branch -d refuses to delete unmerged work — that refusal is useful information
  • git branch -D overrides the check; confirm with git log main..<branch> first
  • Deleting a local branch does nothing to the remote; git push origin --delete <branch> does that

Quiz

Which command lists the branches that are safe to delete?

git branch -d test/experiment fails with "not fully merged". What does that tell you?

Which is the best branch name for testing ticket PAY-4417 about an expired token?

You deleted test/old-suite locally. What happened on the remote?