How to Delete a Local Git Branch

Cleaning up old or unused branches is a good Git practice to keep your workspace organized. Git provides simple commands to safely remove local branches that you no longer need.


🧭 1. List All Local Branches

First, see your local branches:

git branch

Example output:

  main
* feature/login
  bugfix/header

The * indicates your current branch. You cannot delete the branch you’re currently on.


🗑️ 2. Delete a Local Branch

To delete a local branch (e.g., feature/login), run:

git branch -d feature/login
  • -d deletes the branch only if it has been fully merged into your current branch or another target branch.

❗ 3. Force Delete (if Not Merged)

To delete a branch regardless of its merge status (use with caution):

git branch -D feature/login
  • -D is a shortcut for --delete --force

⚠️ Safety Check

Always make sure the branch is no longer needed and that any important changes are merged or pushed before deleting.


📌 Summary

TaskCommand
List local branchesgit branch
Delete merged branchgit branch -d branch-name
Force delete unmerged branchgit branch -D branch-name
Sharing Is Caring:

Leave a Comment