How to Remove a Local Git Branch

Over time, your Git repository may accumulate old or unused branches. Cleaning them up helps keep your project organized and focused.


🛠️ Command to Delete a Local Branch

git branch -d branch-name
  • This deletes the local branch only if it has already been merged.
  • Replace branch-name with the name of the branch you want to delete.

✅ Example

git branch -d feature/login

This deletes the feature/login branch if it has been merged into your current branch.


⚠️ Force Delete an Unmerged Branch

If the branch has not been merged, use -D (capital D) to force delete:

git branch -D branch-name

Example:

git branch -D feature/experimental

⚠️ Warning: This permanently deletes the branch locally. Changes not pushed or merged will be lost.


📋 Check Available Local Branches

Before deleting, you can list all local branches:

git branch

🔄 Optional: Remove Remote-Tracking Branches Too

If you want to delete a local reference to a remote branch (after it has been deleted on the server):

git fetch -p

This prunes stale branches that no longer exist on the remote.


🧠 Summary Table

CommandPurpose
git branch -d branch-nameDelete merged local branch
git branch -D branch-nameForce delete unmerged local branch
git branchList all local branches
git fetch -pPrune deleted remote-tracking branches
Sharing Is Caring:

Leave a Comment