Learn how to delete Git branches in GitHub, GitLab, Azure DevOps, and Bitbucket with easy step-by-step instructions and screenshots. This guide also covers auto-delete after merge, branch cleanup, permissions, and common branch deletion issues.
Table of Contents
Keeping a repository clean is one of those things every team knows they should do — and few actually do consistently. Stale branches accumulate over time, making it harder to navigate a project and understand what’s actively in development. Whether you’re a solo developer or part of a large engineering team, knowing how to properly delete branches in GitHub, GitLab, Azure DevOps, and Bitbucket is a fundamental maintenance skill.
This guide covers everything: deleting branches from the UI, from the command line, setting up automatic cleanup after merges, dealing with protected branches, and recovering branches you deleted by mistake.
How to Delete a Branch in GitHub
GitHub
GitHub provides several ways to perform a GitHub delete branch operation. The most common are through a merged pull request, through the Branches management page, or via the GitHub CLI. Each method is appropriate for different workflows.
Delete a Branch from a Pull Request
The fastest and most common workflow — once a pull request is merged, GitHub offers a one-click option to remove the source branch immediately.
- Navigate to the Pull Requests tab in your repository.
- Open the merged pull request you want to clean up.
- Scroll to the bottom and click “Delete branch” — it appears automatically after the merge is complete.
Success Output
Restore this branch if it was deleted by mistake (available for 30 days)
To delete a remote branch via the command line, use the git push command with the --delete flag:
# Delete a remote branch on GitHub
git push origin --delete feature/user-auth
# Or the equivalent shorthand
git push origin :feature/user-auth
# Also delete the local tracking branch
git branch -d feature/user-authDelete a Branch from the Branches Page
If you need to delete branches in bulk or outside of a pull request context, the Branches page gives you a full view of all branches.
- Go to your repository and click the branch dropdown (shows your current branch name) near the top left.
- Click “View all branches” at the bottom of the dropdown.
- On the Branches page, find the branch to delete and click the 🗑 trash icon on the right.
- Confirm the deletion when prompted.
Filtering Stale Branches
Why GitHub May Prevent Branch Deletion
There are a few reasons a GitHub delete branch operation might be blocked:
- Default branch protection —
mainormastercannot be deleted without first changing the default branch in repository settings. - Branch protection rules — any branch matching a protection rule pattern (e.g.,
release/*) is protected from deletion unless the rule is modified. - Insufficient permissions — only users with Write access or higher can delete branches; Read-only collaborators cannot.
- Open pull request — GitHub will warn you if an open PR targets the branch you’re trying to delete.
Cannot Delete Default Branch
Restore a Deleted GitHub Branch
Deleted a branch by accident? GitHub keeps deleted branches recoverable for up to 30 days.
- Go to the merged Pull Request that was associated with the branch.
- At the bottom of the PR page, click “Restore branch”.
If there was no pull request, you can restore via the git reflog from a local clone:
# Find the last commit of the deleted branch
git reflog
# Recreate the branch at that commit SHA
git checkout -b feature/user-auth a1b2c3d4
# Push it back to GitHub
git push origin feature/user-authHow to Automatically Delete Branches After Merge in GitHub
The GitHub auto delete branch after merge feature is one of the most underused repository settings. Enabling it removes the need for anyone to manually clean up branches after pull requests are merged — the platform handles it automatically.
Enable Auto Delete Branch in Repository Settings
- Go to your repository on GitHub.
- Click Settings in the top navigation bar.
- Scroll down to the Pull Requests section.
- Check the box next to “Automatically delete head branches”.
Setting Enabled
Head branches will be automatically deleted after pull requests are merged.
Once enabled, GitHub will automatically delete the source (head) branch any time a pull request is merged into the repository. The option to restore still appears on the merged PR page for 30 days.
Why Auto Delete Helps Keep Repositories Clean
📌 Real-world Scenario: Stale Branch Accumulation Common Problem
Enabling auto delete after merge at the repository level takes 10 seconds and prevents this entirely. Every merged PR automatically removes its source branch, keeping the branch list short and meaningful.
GitHub Permissions Required for Auto Delete
To toggle the auto delete branch setting, a user needs Admin access to the repository. Regular contributors with Write access cannot change repository settings.
| Role | Delete Own Branch | Delete Any Branch | Enable Auto Delete |
|---|---|---|---|
| Read | ✗ | ✗ | ✗ |
| Triage | ✗ | ✗ | ✗ |
| Write | ✓ | ✓ (non-protected) | ✗ |
| Maintain | ✓ | ✓ | ✗ |
| Admin | ✓ | ✓ | ✓ |
How to Delete a Branch in GitLab
GitLab
The GitLab delete branch process closely parallels GitHub’s, but there are some platform-specific settings worth knowing — especially around protected branch rules and auto-deletion options.
Delete a GitLab Branch from Merge Requests
GitLab allows you to delete a source branch as part of the merge flow, either at merge time or afterward.
- Open the Merge Request in your GitLab project.
- Before merging, check “Delete source branch when merge request is accepted” — this is configurable per MR.
- Click Merge. GitLab will merge and immediately delete the source branch.
GitLab Default Setting
# Delete a remote branch on GitLab
git push origin --delete feature/payment-api
# Using the GitLab CLI (glab)
glab repo branch delete feature/payment-api
# Clean up stale remote-tracking refs locally
git remote prune originRemove Branches from Repository Settings
- In your GitLab project, navigate to Repository → Branches in the left sidebar.
- Use the search field to filter branches. GitLab also has a “Stale branches” filter.
- Click the trash icon next to any branch you want to delete.
- GitLab will ask for confirmation before deleting.
Protected Branch Restrictions in GitLab
GitLab’s protected branch system is more granular than GitHub’s. A branch can be protected against both pushing and deletion independently. To delete a protected branch:
- Go to Settings → Repository → Protected branches.
- Find the protected branch rule for the branch in question.
- Click Unprotect or modify the rule to allow deletion for Maintainers.
- Return to the Branches page and delete normally.
GitLab Roles Required
How to Delete a Branch in Azure DevOps
Azure DevOps
For teams using Azure DevOps, performing an Azure DevOps delete branch is handled through Azure Repos. The process supports both UI-based deletion and command-line approaches via the Azure CLI or git directly.
Delete a Branch from Azure Repos
- In your Azure DevOps project, go to Repos → Branches.
- Search for or scroll to the branch you want to remove.
- Click the ⋯ (More options) button on the right side of the branch row.
- Select “Delete branch” from the dropdown menu.
# Standard git approach (works with any remote, including Azure)
git push origin --delete feature/reporting-dashboard
# Using the Azure CLI (az devops extension)
az repos ref delete \
--name refs/heads/feature/reporting-dashboard \
--repository MyRepo \
--project MyProject \
--organization https://dev.azure.com/MyOrg
# Prune stale references locally
git fetch --prune originRemove Completed Feature Branches After Pull Requests
Azure DevOps can be configured to automatically delete source branches when pull requests are completed. This is done per-pull request or as a repository-level default.
🔧 Setting Auto Delete on a Pull Request Azure PR Settings
To make this the default for all PRs in a repository, navigate to Project Settings → Repositories → Policies and enable “Delete source branch” as a default completion option.
Branch Policies That Block Deletion
Azure DevOps branch policies are among the most comprehensive of any Git platform. Policies that can block an Azure DevOps delete branch operation include:
- Minimum reviewers — branches with active PRs requiring a minimum number of reviewers cannot have their target branch deleted.
- Linked work items — policies that enforce work item linking may prevent deletion if linked items are still open.
- Build validation — if a branch has an active build policy, it is generally considered production-critical and protected.
- Lock branch — a branch can be explicitly locked by a repo admin, preventing any pushes or deletions.
Locked Branches in Azure
How to Delete a Branch in Bitbucket
Bitbucket
The Bitbucket delete branch process works through the Source view, pull request completion, or the Bitbucket CLI. Bitbucket Cloud and Bitbucket Data Center differ slightly in their interfaces, though the core concept is the same.
Delete Branches in Bitbucket Cloud
- Navigate to your Bitbucket repository.
- Click Source in the left sidebar, then select Branches from the branch dropdown.
- Click the ⋯ icon next to the branch you want to delete.
- Select “Delete branch” and confirm.
# Delete remote branch on Bitbucket (standard git)
git push origin --delete feature/checkout-flow
# Using the Bitbucket CLI
bb branch delete feature/checkout-flow
# Also remove local tracking reference
git branch -dr origin/feature/checkout-flowDelete Merged Branches Safely
Bitbucket Cloud allows branch deletion directly from a merged pull request, similar to GitHub. When a PR is merged, Bitbucket displays a “Delete branch” button in the PR overview. This is the safest way to delete a branch because:
- The merge is confirmed before deletion — you won’t accidentally delete an unmerged branch.
- Bitbucket keeps a record in the PR audit log that the branch was deleted post-merge.
- You can restore the branch from the same PR page if needed.
Bitbucket Branch Permissions Explained
Bitbucket Cloud uses branch permissions to control who can read, write to, and delete specific branches. These are configured at the repository level.
- Go to Repository Settings → Branch permissions.
- Click Add a branch permission.
- Set a branch pattern (e.g.,
main,release/*) and configure which users or groups can delete it.
Bitbucket Branch Permission Model
develop or staging.Common Branch Deletion Problems and Fixes
Whether you’re working with GitHub, GitLab, Azure, or Bitbucket, the same categories of issues tend to block branch deletion. Here’s how to diagnose and resolve each one.
Cannot Delete Protected Branch
🛡️ Problem: Branch is Protected Blocked
main, master, develop, or any branch matching a wildcard pattern like release/*.Fix: Go to repository settings → Branch protection rules (GitHub) / Protected branches (GitLab) / Branch policies (Azure) / Branch permissions (Bitbucket). Either remove the rule or exempt the specific user who needs to delete the branch.
Error
error: failed to push some refs to ‘git@github.com:org/repo.git’
Missing Permissions to Delete Branch
🔐 Problem: Insufficient Access Rights Permissions
Fix: Request the repository admin to elevate your access, or ask them to perform the deletion on your behalf. In GitHub and Bitbucket, repository owners can also grant Write access to specific collaborators without making them full administrators.
Branch Still Appears After Deletion
If you’ve deleted a remote branch but it still shows up in your local git branch list, your local repository hasn’t pruned its stale remote-tracking references.
# Fetch and prune deleted remote branches
git fetch --prune origin
# Or configure git to prune automatically on fetch
git config --global fetch.prune true
# List all local tracking branches that no longer exist remotely
git branch -vv | grep ': gone]'
# Delete all gone tracking branches in one step
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -dRemote Branch Was Already Removed
When you try to delete a remote branch that was already deleted by a teammate, git returns a cryptic error:
Error
error: failed to push some refs to ‘git@github.com:org/repo.git’
This is harmless — the branch is already gone from the remote. Run git fetch --prune origin to sync your local reference list and the error will resolve itself.
Best Practices for Branch Cleanup
Delete Feature Branches After Merge
Protect Long-Lived Branches
main, develop, and any release branches. These should never be accidentally deleted.Enable Auto Delete at Repo Level
Schedule Quarterly Branch Audits
Use Branch Naming Conventions
feature/, fix/, hotfix/ makes it much easier to identify which branches are candidates for cleanup.Configure git fetch.prune Globally
git config --global fetch.prune true on all developer machines so local tracking branches stay synchronized with the remote automatically.Delete Feature Branches After Merge
The single most impactful habit for repository hygiene is establishing a team norm that every feature branch gets deleted after its PR is merged. Whether you do this manually by clicking “Delete branch” or automatically via repo settings, the result is the same: a branch list that only contains branches representing active, ongoing work.
Keep Main and Production Branches Protected
Your main, master, production, and release/* branches should always have deletion protection enabled. On every platform covered in this guide, this takes fewer than five minutes to configure and can prevent a catastrophic accidental deletion.
Use Auto Delete for Cleaner Repositories
The GitHub auto delete branch after merge feature — and equivalent settings in GitLab, Azure DevOps, and Bitbucket — effectively enforces the “delete after merge” practice automatically, removing the human memory requirement from the equation. For most teams, enabling this is the single highest-leverage improvement to their branch hygiene workflow.
Recommended Baseline Settings
main and any release branches from direct deletion, configure fetch.prune true for all contributors, and establish a 90-day stale branch review cadence.Frequently Asked Questions
Does deleting a branch delete commits?
main) are permanently safe.Can I recover a deleted branch?
git reflog to find the last commit SHA of the deleted branch and recreate it with git checkout -b branch-name SHA.Who can delete branches in GitHub or Azure DevOps?
In Azure DevOps: Users with the Contribute permission on a repository can delete branches. Protected branches (those with branch policies applied) require a user with the Exempt from policy enforcement permission or an administrator to bypass the policy.
Should merged branches be deleted?
v1-lts) that you plan to keep active for hotfixes over an extended period.Conclusion
Deleting branches is a small action with a meaningful cumulative impact on repository health. Whether you’re working in GitHub, GitLab, Azure DevOps, or Bitbucket, the core workflow is straightforward — but knowing where to find the settings, how to handle permissions, and what to do when things go wrong is what separates a well-maintained codebase from a tangled one.
Quick Recap of Branch Deletion Methods
| Tool | How to delete | Has Auto delete |
|---|---|---|
| 🐙 GitHub | Via PR “Delete branch” button, Branches page trash icon, or git push origin --delete | Auto delete ✓ |
| 🦊 GitLab | Via MR completion checkbox, Repository → Branches trash icon, or glab repo branch delete | Auto delete ✓ |
| ☁️ Azure DevOps | Via Repos → Branches ⋯ menu, PR completion modal, or az repos ref delete | Auto delete ✓ |
| 🪣 Bitbucket | Via Source → Branches ⋯ menu, PR “Delete branch” button, or bb branch delete | Auto delete ✓ |
Recommended Cleanup Workflow for Teams
- Enable auto delete after merge in repository settings on every platform (Admin access required).
- Add branch protection rules to
main,develop, and all release branches to prevent accidental deletion. - Configure
git config --global fetch.prune trueon every developer machine to keep local refs synchronized. - Run a quarterly stale branch audit using each platform’s “Stale branches” filter to catch any branches that auto delete may have missed.
- Document your branch naming convention and deletion policy in your team’s CONTRIBUTING.md so new engineers follow the same standards from day one.
Also read How to create branch in Git from current or another branch, Commit, Tag & Remote.



