Delete Git Branch (Quick Answer)
Delete local branch (safe)
Use the following command to safely delete a local Git branch created using
git branch:
git branch -d branch_nameThis command deletes the branch only if it has been fully merged. It helps prevent accidental data loss.
Force delete local branch
If the branch has unmerged changes and you still want to delete it:
git branch -D branch_nameThis force deletes the branch without checking merge status. Use with caution.
Delete remote branch (GitHub / GitLab)
To delete a branch from a remote repository using
git push:
git push origin --delete branch_nameThis works for both GitHub and GitLab repositories.
Delete both local and remote branch
Recommended workflow when managing branches:
git push origin --delete branch_name
git branch -D branch_nameFirst delete from remote, then remove it locally to keep everything clean.
What Happens When You Delete a Git Branch?
Does deleting a branch remove commits?
No. Deleting a branch does not immediately delete commits.
Commits remain in Git history until they are garbage collected and can be recovered using git reflog. If no references exist, they may eventually be removed.
Local vs remote branch deletion explained
- Local branch → Deleted only from your system
- Remote branch → Deleted from shared repository (GitHub/GitLab)
Deleting one does not automatically delete the other.
Can deleted branches be recovered?
Yes, in most cases.
Use
git reflog:
git reflogFind the commit hash and recreate the branch:
git branch branch_name <commit_hash>How to Delete Local Git Branch
Delete branch using git branch -d
Safe deletion:
git branch -d feature-branchGit will prevent deletion if the branch contains unmerged changes, which you can review using git diff examples.
Force delete branch using git branch -D
Force deletion:
git branch -D feature-branchUse this when you are sure the changes are not needed.
Delete multiple local branches
To delete multiple branches at once:
git branch -d branch1 branch2 branch3To force delete multiple branches:
git branch -D branch1 branch2 branch3This helps clean up multiple feature branches quickly.
How to Delete Remote Git Branch
Delete remote branch using git push --delete
To delete a branch from a remote repository (GitHub or GitLab):
git push origin --delete branch_nameThis command removes the branch from the remote repository but does not affect your local branch.
Alternative syntax for older Git versions
Older Git versions use this syntax:
git push origin :branch_nameBoth commands achieve the same result, but --delete is the modern and recommended approach.
Verify remote branch deletion
To confirm that the branch is deleted from remote, list remote branches using git branch command:
git branch -rOr fetch updated references using
git fetch:
git fetch --all --pruneHow to Delete Branch in GitLab (UI + CLI)
Delete branch from GitLab UI
Steps:
- Open your project in GitLab
- Go to Repository → Branches
- Find the branch
- Click Delete next to the branch
Note: You cannot delete protected branches from UI unless protection is removed.
Delete branch using command line
Use the same Git command:
git push origin --delete branch_nameThis works for GitLab repositories as well.
GitLab protected branch restrictions
If you see an error while deleting:
- The branch may be protected (e.g.,
main,master) - Only maintainers or admins can delete protected branches
- You must unprotect the branch in GitLab settings before deletion
Delete Local and Remote Branch Together
Recommended workflow (remote first, then local)
Best practice:
git push origin --delete branch_name
git branch -D branch_nameDeleting remote first ensures no one else uses the branch.
Clean up remote tracking branches (git fetch --prune)
After deleting remote branches, clean stale references using git fetch:
git fetch --pruneThis removes outdated remote-tracking branches like origin/branch_name.
Common Errors While Deleting Git Branch
Cannot delete branch checked out error
Error:
error: Cannot delete branch 'branch_name' checked outFix:
git switch main
git branch -d branch_nameYou must switch to another branch before deleting using
git switch or
git checkout.
Branch is not fully merged error
Error:
error: The branch 'branch_name' is not fully mergedFix options:
- Merge branch first using
git merge:
git merge branch_name
git branch -d branch_name- Or force delete:
git branch -D branch_nameCannot delete branch used by worktree
Error occurs when branch is used by another worktree (see Git Worktree concepts via checkout/switch).
Fix:
git worktree list
git worktree remove <path>Then delete the branch.
Remote branch not found error
Error:
error: unable to delete 'branch_name': remote ref does not existFix:
- Verify branch name
- Fetch latest refs:
git fetch --all --prune- Check remote branches:
git branch -rFix: Cannot Delete Branch Checked Out
Switch to another branch before deletion
You cannot delete a branch if you are currently on it.
Fix this by switching to another branch:
git switch mainOr:
git checkout mainAfter switching, delete the branch:
git branch -d branch_nameVerify current branch
Check which branch you are on:
git branchThe current branch is marked with *. Make sure it is not the one you want to delete.
Fix: Branch Not Fully Merged Error
Merge branch before deleting
If Git prevents deletion due to unmerged changes:
git merge branch_name
git branch -d branch_nameThis ensures changes are safely preserved before deletion.
Force delete when safe
If you are sure the changes are not needed:
git branch -D branch_nameThis force deletes the branch without checking merge status.
Advanced Branch Cleanup
Delete all local branches except main
To delete all branches except main:
git branch | grep -v "main" | xargs git branch -DReplace main with your default branch if different.
Delete merged branches only
Clean up only merged branches safely:
git branch --merged | grep -v "\*" | xargs git branch -dThis removes only branches that are already merged.
Clean stale remote branches
Remove deleted remote branch references:
git fetch --pruneThis keeps your repository clean and up to date.
Best Practices for Git Branch Deletion
Always verify branch before deleting
List branches before deletion:
git branch -aEnsure you are deleting the correct branch.
Frequently Asked Questions
1. How do I delete a branch in Git?
You can delete a local branch usinggit branch -d branch_name. To delete a remote branch, use git push origin --delete branch_name.2. How do I force delete a Git branch?
Usegit branch -D branch_name to force delete a local branch even if it has unmerged changes.3. Why can't I delete a Git branch?
You cannot delete a branch if you are currently on it or if it has unmerged changes. Switch to another branch or use force delete with-D.4. How do I delete a remote branch in GitLab or GitHub?
Usegit push origin --delete branch_name to remove the branch from remote repositories like GitHub or GitLab.5. What is the difference between git branch -d and -D?
-d safely deletes a branch only if it is fully merged, while -D force deletes the branch regardless of merge status.6. Can I recover a deleted Git branch?
Yes, you can recover a deleted branch usinggit reflog to find the last commit and recreate the branch using git branch branch_name <commit-hash>.7. How do I delete both local and remote branches?
First delete the remote branch usinggit push origin --delete branch_name, then delete the local branch using git branch -D branch_name.Summary
In this guide, you learned how to delete Git branches locally and remotely using safe and force methods. We covered commands like git branch -d, git branch -D, and git push origin --delete, along with real-world scenarios and error fixes.
You also explored how to resolve common issues such as deleting a checked-out branch, handling unmerged changes, and cleaning up stale branches. Advanced cleanup commands and best practices help maintain a clean and efficient Git workflow.
By following these methods, you can safely manage and delete Git branches in both local and remote repositories like GitHub and GitLab.




![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)





