How to Delete a Git Remote Branch: Steps for Both Local and Remote

A practical guide to deleting Git remote branches — remote deletion with git push origin --delete, local deletion with git branch -d, removing remote repositories themselves, and how to recover a deleted branch.
代表 / エンジニア
As team development progresses, merged branches tend to pile up in the remote repository. Left alone, the branch list gets cluttered and it becomes harder to tell which branches are still active.
This article covers how to delete Git remote branches, and systematically walks through deleting local branches, disconnecting from a remote repository, and restoring deleted branches — covering the operations you actually need in practice.
The Command to Delete a Remote Branch
To delete a remote branch, run git push with the --delete option:
git push origin --delete feature/old-branchThis deletes feature/old-branch from the remote repository (origin). After running it, when other team members run git fetch --prune, their local remote-tracking branches disappear automatically too.
An older form that does the same thing:
# Empty before the colon = pushing "nothing" to the remote = delete
git push origin :feature/old-branchThis syntax is from the early days of Git but isn't intuitive, so I recommend using --delete.
⚠ Caution: Deleting a remote branch affects the whole team. Make sure it's been merged before running this.
Safety Check Before Deletion
To check whether the target branch has been merged:
# List branches merged into main
git branch -r --merged mainBranches in this list are safe to delete. To see unmerged branches, use --no-merged:
# List branches NOT merged into main
git branch -r --no-merged mainHow to Delete a Local Branch
Separate from deleting the remote branch, you also need to clean up the local branch.
# Delete a merged local branch
git branch -d feature/old-branch
# Force-delete an unmerged branch
git branch -D feature/old-branchLowercase -d only deletes merged branches and errors on unmerged ones. This is Git's safety mechanism — it prevents you from losing work accidentally.
Uppercase -D force-deletes. It deletes even if there are unmerged changes, so verify first.
Bulk-Delete Merged Local Branches
When many branches have been merged, this one-liner deletes them all at once:
# Bulk-delete local branches merged into main (excluding main itself)
git branch --merged main | grep -v "main" | xargs git branch -dThis command comes up often, so aliasing it is convenient:
git config --global alias.cleanup '!git branch --merged main | grep -v "main" | xargs git branch -d'
# Now run it as:
git cleanupDeleting Both Remote and Local Branches
In practice, most of the time you're deleting both the remote and local branch simultaneously. Run these in order:
# 1. Delete the remote branch
git push origin --delete feature/old-branch
# 2. Delete the local branch
git branch -d feature/old-branch
# 3. Clean up remote-tracking references
git fetch --pruneLearn this three-step sequence as a set, and branch cleanup goes efficiently.
Removing a Remote (Disconnecting from a Remote Repository)
People searching for "git remote delete" often actually want to remove the connection to the remote repository, not delete a remote branch.
# Check registered remotes
git remote -v
# Remove the remote repository connection
git remote remove origingit remote remove (or git remote rm) deletes the reference from the local repository to the remote repository. The remote repository itself is not deleted.
A common use case: after cloning from a forked repository, re-setting your own repository as origin.
# Remove the old origin
git remote remove origin
# Set the new origin
git remote add origin git@github.com:your-account/your-repo.git
# Confirm
git remote -vTo Delete the Remote Repository Itself
If you want to delete the GitHub or GitLab repository itself, use the platform's web UI rather than Git commands.
- GitHub: Settings → Danger Zone → Delete this repository
- GitLab: Settings → General → Advanced → Delete project
⚠ Caution: Repository deletion is irreversible. All team access is lost, and issues, pull requests, wikis, and more are all deleted. Take a backup before running this.
Cleaning Up Local References to Deleted Remote Branches
In team development, when other members delete remote branches, references to them (origin/feature/...) linger in your local.
# Clean up references to deleted remote branches in one step
git fetch --prune
# Short form
git fetch -pThe --prune option automatically removes local references to branches that no longer exist on the remote.
If running it manually each time is tedious, you can configure fetch to prune automatically:
git config --global fetch.prune trueWith this setting, running git fetch alone cleans up stale remote-tracking branches automatically. It's an extremely useful setting for team development.
How to Recover a Deleted Branch
"I accidentally deleted the branch" happens in team development. Git retains most data internally, so recovery is often possible.
Recovering a Local Branch
Use git reflog to find the last commit hash of the deleted branch.
# Search reflog for commits from the deleted branch
git reflog
# Recreate the branch from the found commit hash
git checkout -b feature/old-branch abc1234reflog keeps 90 days of history by default, so unless it's really old, recovery is possible.
Recovering a Remote Branch
If you've just deleted a remote branch, it may still exist on a teammate's local. Having them push it is the simplest path.
If no one has it locally, recover from your own reflog and push it again:
# Find the commit hash in reflog
git reflog
# Recreate the local branch
git checkout -b feature/old-branch abc1234
# Push it to remote again
git push origin feature/old-branchBest Practices for Branch Deletion in Team Development
Deleting branches is an action that affects the whole team. Having these operational rules in place prevents trouble.
Enable automatic deletion after merge: In GitHub's Pull Request settings, turn on "Automatically delete head branches" — merged remote branches are deleted automatically. The simplest and most reliable option.
Standardize branch naming: Agree on prefixes like feature/, bugfix/, and hotfix/ across the team to make deletion targets easy to identify.
Periodic branch inventory: Monthly or biweekly bulk deletion of merged branches is effective. The git cleanup alias above shines here.
Protected branch settings: For critical branches like main/develop, configure GitHub's Branch Protection Rules to prevent accidental deletion.
Summary
A summary of Git remote branch deletion operations:
| Goal | Command |
|---|---|
| Delete remote branch | git push origin --delete branch-name |
| Delete local branch | git branch -d branch-name |
| Force delete (unmerged) | git branch -D branch-name |
| Remove remote connection | git remote remove origin |
| Clean up stale remote references | git fetch --prune |
| Recover a deleted branch | git checkout -b branch-name <commit-hash> |
Branch cleanup is unglamorous work, but it's an important operational habit that keeps team development visibility high and makes day-to-day Git operations smoother.
For fetching remote branches, see How to Fetch and Check Out Remote Branches in Git. For deleting files from a Git repository, see How to Delete Files from a Git Repository.
At aduce Inc., we provide IT advisory support that includes standardizing Git workflows. If you'd like to improve your team's Git workflow, please feel free to reach out via Contact.