AI Hints
Home›Git & GitHub Tutorial›Working with Branches
Git & GitHub · Lesson 43 of 144

Listing and Deleting Branches

Git branch -d.

⏱ 2 min read▶ Video at 46:05🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Branches pile up quickly. Old experiments, finished features. Let's see how to list them with details, rename them, and delete the ones you don't need.

Listing and Deleting Branches
From the video: Listing and Deleting Branches
  • Branches pile up
  • List, rename, delete
  • Keep things tidy

More detail

Git branch, dash v, shows each branch with its latest commit. The star marks where we are. To rename a branch, use git branch dash m, the old name, and the new name. Here, test becomes experiment.

git
git branch -v
  contact f63a17a Link to about page
* main    f63a17a Link to about page
  test    f63a17a Link to about page
git branch -m test experiment
git branch
  contact
  experiment
* main

Deleting

To delete a branch, use git branch dash d. The contact branch was never used, so Git deletes it straight away. But experiment has a commit that's not merged anywhere. So Git refuses, to protect your work. If you really want to throw it away, use capital D.

git
git branch -d contact
Deleted branch contact (was f63a17a).
git branch -d experiment
error: The branch 'experiment' is not fully merged.
If you are sure you want to delete it, run 'git branch -D experiment'.
git branch -D experiment
Deleted branch experiment (was 6080d13).

Summary

Git branch dash v lists with details, dash m renames, dash d deletes safely, and capital D forces it. Next, bringing a branch's work back into main.

Key takeaways
  • git branch -v
  • git branch -m old new
  • -d safe, -D force
Next up: Merging — git merge