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
Command | Purpose |
---|---|
git branch -d branch-name | Delete merged local branch |
git branch -D branch-name | Force delete unmerged local branch |
git branch | List all local branches |
git fetch -p | Prune deleted remote-tracking branches |