How to Delete a Git Branch: Local, Remote, and Everything In Between

Git branches are cheap to create — and that's the point. You spin one up for a feature, a bug fix, or an experiment, then merge it when you're done. But over time, stale branches accumulate, cluttering your repository and making collaboration harder. Knowing how to delete them cleanly, in the right context, is a core Git skill.

Why Branch Cleanup Matters

A repository with dozens of merged, abandoned, or duplicate branches creates real friction. Developers lose time scanning branch lists. CI/CD pipelines can trigger on branches that should no longer exist. Remote repositories shared across teams become harder to navigate. Deleting branches you no longer need is part of responsible repository hygiene — not just tidying for its own sake.

Git treats local branches and remote branches as separate things, which is why the deletion process involves different commands depending on what you're cleaning up.

Deleting a Local Git Branch

A local branch exists only on your machine. Once you've merged your work (or decided to abandon it), you can remove it with:

git branch -d branch-name 

The -d flag stands for delete, but it includes a safety check: Git will refuse to delete the branch if it hasn't been fully merged into your current branch or upstream. This protects you from accidentally losing unmerged work.

If you're certain you want to delete the branch regardless of its merge status — for example, you've abandoned the work intentionally — you can force the deletion:

git branch -D branch-name 

The capital -D is shorthand for --delete --force. Use it deliberately. There's no undo for a deleted unmerged branch unless you know the commit hash and can recover it with git reflog.

Before deleting, make sure you're not currently on the branch you're trying to remove. Switch to main, master, or another branch first:

git checkout main git branch -d branch-name 

Deleting a Remote Git Branch

Remote branches live on a server — typically GitHub, GitLab, Bitbucket, or a self-hosted Git instance. Deleting a local branch does not remove the corresponding remote branch. You have to do that separately.

The modern syntax for deleting a remote branch is:

git push origin --delete branch-name 

origin is the conventional name for your primary remote. If your remote has a different name, substitute accordingly. This command tells the remote server to remove that branch reference entirely.

An older syntax you may still see in documentation or tutorials:

git push origin :branch-name 

The colon syntax pushes "nothing" to the remote branch, effectively deleting it. Both forms work, but --delete is more readable and should be preferred.

Cleaning Up Remote-Tracking References 🧹

After deleting remote branches, your local repository may still hold remote-tracking references — stale pointers like origin/branch-name that no longer correspond to anything on the server. These don't cause bugs, but they add noise.

To remove them, run:

git fetch --prune 

This syncs your local remote-tracking references with the actual state of the remote, deleting any that no longer exist. You can also configure Git to prune automatically on every fetch:

git config --global fetch.prune true 

With this set, git fetch will always clean up stale remote-tracking references without you needing to remember the flag.

Deleting Multiple Branches at Once

Git's branch deletion commands accept multiple branch names in one line:

git branch -d feature-login feature-signup hotfix-404 

For more systematic cleanup, you can combine Git commands with shell tools to delete all branches that have already been merged into main:

git branch --merged main | grep -v "^* |main" | xargs git branch -d 

This pipeline lists merged branches, filters out main and your current branch, then passes the rest to git branch -d. It's a common maintenance pattern on long-running projects, but review the output carefully before running it in production repositories.

What Varies by Setup and Workflow

How and when you delete branches depends heavily on your environment and team conventions:

FactorImpact on Deletion Approach
Git hosting platformGitHub and GitLab offer auto-delete on merge; Bitbucket and self-hosted instances may not
Branch protection rulesProtected branches (e.g., main, release) often can't be deleted without elevated permissions
Team sizeSolo developers clean up locally; teams need agreed-upon conventions for remote deletion
CI/CD integrationSome pipelines track branches by name; deleting active branches mid-pipeline can cause failures
Git versionThe --delete flag for remote branches was introduced in Git 1.7.0; very old installations may require the colon syntax

The Merge Status Question ⚠️

The most consequential variable in branch deletion is whether the branch has been merged. Git's -d flag enforces this for local branches, but there's no equivalent safety check built into git push --delete for remote branches. You're trusting your own judgment — or your team's process — that the remote branch is safe to remove.

Some teams address this by requiring pull requests and enabling automatic branch deletion after merge at the platform level. Others rely on naming conventions, protected branches, or scheduled cleanup scripts. There's no universally correct approach.

Whether you're working solo or on a distributed team, the right deletion workflow depends on how your repository is structured, what your CI/CD system expects, and how much tolerance your project has for recovering accidentally deleted work.