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