Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Branching Basics  ›  Lesson

Renaming and Deleting Branches

Deleting Branch 12 mingit branch -m renames a branch, git branch -d deletes it only if its work is merged, and -D forces the delete.

Renaming and Deleting Branches

Asha's experiments leave a trail: test, vegan-pancakes, pancake-v2. Some names were vague and some branches are finished. Tidying them keeps git branch readable, and Git has a safety catch so tidying never costs you work.

The rule

Rename with -m; delete with -d, which refuses to lose unmerged work.

Renaming. A branch name is only a label, so renaming is instant and changes no commits:

git branch -m better-name          # rename the branch you are on
git branch -m test oat-pancakes    # rename another branch: old, then new

A common real rename: older repositories often start on a branch called master. If you would rather match the main used everywhere else, git branch -m master main renames it in one step, and all its commits stay exactly as they were.

Deleting. Once a branch's work is merged, the label is no longer needed:

git branch -d vegan-pancakes

-d is the safe delete. If the branch has commits that are not merged into your current branch (or its upstream), Git refuses with a message that the branch is not fully merged. That protects work you may have forgotten.

When you truly want to throw an experiment away, use the capital letter:

git branch -D pancake-v2           # force delete, merged or not

Two more rules:

  • You cannot delete the branch you are currently on. Switch to another branch first.
  • Deleting a branch removes the label, not the commits that other branches still point to. Merged work stays in history.

Branches on a remote. Everything above is local. If a branch was shared on GitHub, renaming or deleting it locally does not change the remote copy. You will meet remotes properly in a later section; for now, know that the remote side needs its own step, such as:

git push origin --delete vegan-pancakes

The shortcut

Before deleting, ask "Is this work anywhere else?" Run git branch --merged to list branches whose commits are already inside the current branch. Those are safe for -d. Anything missing from that list needs a decision: merge it, or knowingly force-delete it with -D.

Spring cleaningStep 1 of 4
  1. Stand on main

    git switch main, because you cannot delete the branch you are on.

The trap is reaching for -D by habit. The refusal from -d is Git asking "are you sure?"; read it before overriding it.

Drills

Answer fast — trust the rule.

quick drill

Which command renames the current branch to oat-pancakes?

quick drill

git branch -d pancake-v2 fails with "not fully merged". What does that mean?

quick drill

Asha is on vegan-pancakes and runs git branch -d vegan-pancakes. What happens?

quick drill

Asha deletes a local branch that also exists on GitHub. What happens to the GitHub copy?

Remember

  • Rename with -m; delete with -d, which refuses to lose unmerged work.
  • Check git branch --merged first; force with -D only on purpose.
  • You cannot delete the branch you are on, and remote branches need their own step.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainClean up without losing work

Asha has branches main, vegan-pancakes (merged) and pancake-v2 (not merged), and she is currently on vegan-pancakes. Describe how she should clean up, and explain the difference between -d and -D.