🎓 A1Learner
🔍
0 of 50 lessons completed

Delete a Branch

Delete removes a branch you no longer need. It removes the name, not the snapshots, when the work is saved elsewhere.

Delete a finished branch

The work already sits on main, so -d allows it.

$ git branch -d add-recipe

Force a delete

The branch holds work that main does not have. Only -D removes it.

$ git branch -D experiment

Example

$ git branch
$ git branch -d add-recipe

Key points

  • Delete with -d when the branch’s work already sits on main.
  • Git refuses -d when the work is not on main yet.
  • Delete with -D forces the removal.
  • Forcing throws the work away. Use it with care.
  • You cannot delete the branch you are on.
  • Switch to another branch first.

Here, only main is left before the delete. The branch goes away, and its snapshots stay on main where they were joined.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

When is -d safe to use?