How to Delete Git Branches Locally and Remotely
Git branches act as independent environments for development. They allow you to work on new features, fixes, or experiments without impacting the main project. Each branch keeps its own commit history, making it possible to test safely before merging. Over time, unused or already merged branches may clutter your repository and complicate collaboration. Regular branch cleanup, whether after merging, finishing tasks, or discarding experiments, keeps your project organized and manageable. This guide explains Git branch handling and demonstrates how to remove them locally and remotely.
The Short Answer Version
If you already know Git and need a quick reference for deleting branches, use the following commands:
# Delete a local branch (safe, won’t remove unmerged changes)
$ git branch -d <branch_name>
# Force delete a local branch (removes even if unmerged)
$ git branch -D <branch_name>
# Delete a remote branch
$ git push origin --delete <branch_name>
Keep reading for detailed explanations and examples of each case.
Delete a Branch in Git Locally
Once a feature branch has been merged into the main branch, you can safely remove it from your local repository. Git includes built-in protection to avoid accidental deletion.
Command Explanation (Local)
Use the git branch
command with either -d
or -D
. The lowercase flag only deletes if merged, while the uppercase version forces deletion even when unmerged.
Command Syntax (Local)
To delete a branch locally:
git branch -d <branch_name>
git branch -D <branch_name>
- git branch: Handles branch operations.
- -d: Deletes only if already merged.
- -D: Forcefully deletes regardless of merge state.
- <branch_name>: Name of the branch to delete.
Command Flags Overview (Local)
Additional flags for managing branches:
Flag | Description |
---|---|
-d, –delete | Deletes only if merged |
-D | Force deletes regardless of merge |
-v, –verbose | Shows last commit for each branch |
-a, –all | Lists local and remote branches |
–merged | Lists branches merged into current branch |
–no-merged | Lists branches not yet merged |
Command Demonstration (Local)
Example workflow for deleting local branches:
List all local branches:
$ git branch
development
feature-login
hotfix-header
* main
Check merged branches:
$ git branch --merged
development
feature-login
* main
Delete a merged branch:
$ git branch -d feature-login
Deleted branch feature-login (was 8cd77fa).
Attempt to delete an unmerged branch:
$ git branch -d hotfix-header
error: the branch 'hotfix-header' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D hotfix-header'
Force-delete the unmerged branch:
$ git branch -D hotfix-header
Deleted branch hotfix-header (was a0716a9).
Recheck remaining branches:
$ git branch
development
* main
Deleting a branch only removes its reference. If unmerged, its commits may become unreachable and eventually cleaned by Git’s garbage collector.
Delete a Branch in Git Remotely
After deleting a branch locally, you may also remove it on the remote repository. This keeps the project consistent across all contributors and prevents unnecessary clutter.
Command Overview (Remote)
To remove a remote branch, use git push
with --delete
. Unlike local deletion, Git does not check merge status for remote deletions.
Command Syntax (Remote)
Modern syntax:
git push <remote> --delete <branch_name>
- git push: Updates the remote repository.
- <remote>: Typically “origin”.
- –delete: Requests removal of the branch.
- <branch_name>: The branch to delete.
Legacy alternative syntax:
git push <remote> :<branch_name>
The colon before the branch name signals Git to delete it from the remote repository.
Command Flags Overview (Remote)
When deleting branches on a remote repository, Git offers two approaches: the modern --delete
flag and the older colon-based method. While remote deletions don’t include safeguards for unmerged branches, both commands achieve the same outcome. Here’s a summary of helpful flags for remote deletions:
Flag | Description |
---|---|
–delete, -d | Removes the specified branch from the remote repository |
–force, -f | Forces the push, even if it’s not a fast-forward (not required for delete) |
–dry-run, -n | Simulates the command without making changes |
–verbose, -v | Shows detailed output during the operation |
Command Demonstration (Remote)
Here’s a step-by-step example to delete a remote branch and clean up local references.
List all remote branches:
$ git branch -r
origin/HEAD -> origin/main
origin/development
origin/feature-login
origin/hotfix-header
origin/main
Remove the feature-login
branch from the remote repository:
$ git push origin --delete feature-login
To https://github.com/<username>/<repository>.git
- [deleted] feature-login
Prune deleted references from your local clone:
$ git fetch --prune
From https://github.com/<username>/<repository>.git
- [deleted] (none) -> origin/feature-login
The --prune
option removes remote-tracking references that no longer exist on the server.
Check again to confirm the branch is gone:
$ git branch -r
origin/HEAD -> origin/main
origin/development
origin/hotfix-header
origin/main
Remove your local copy of the deleted remote branch:
$ git branch -d feature-login
Other contributors with local clones also need to run the prune command to update their references:
$ git fetch --prune
Note: To delete a remote branch, you must have the appropriate permissions. If you encounter errors, verify your access rights or contact the repository administrator.
Conclusion
Now you know how to delete Git branches both locally and remotely. Whether you are cleaning up merged branches or removing outdated ones, commands like git branch -d
, git branch -D
, and git push origin --delete
help keep your repository organized. You also learned when deletion makes sense and how to avoid pitfalls such as accidentally force-deleting unmerged work. For additional details and advanced usage, consult the official Git documentation.