Skip to main content

Git Delete Branch: How to Remove Local and Remote Branches

This guide explains how to delete branches in Git—locally and remotely—while following best practices to avoid accidental data loss.
Mar 12, 2025  · 15 min read

If you’re an active Git user like me, you’ve probably accumulated a pile of old branches. Some merged, some abandoned, and some you don’t even remember creating. Keeping those branches around might not seem like a big deal, but over time, they can clutter your repository and make it harder to track active work.

In this guide, I’ll explain how to delete Git branches locally and remotely, covering the safest ways to do it and the potential pitfalls to watch out for.

What Does Deleting A Git Branch Do?

Git branches are temporary by design. They give you a space to work on new features, bug fixes, or experiments without interfering with the main codebase. But once a branch has served its purpose, keeping it around only adds unnecessary clutter. Deleting branches is what I consider good housekeeping to keep your Git workflow clean and manageable.

I’d like to reiterate that “deleting” a branch is not the same as “erasing” your work. When you delete a branch in Git, you’re not erasing the commits, just the reference to them. Here’s how it works:

  • Deleting a local branch removes the branch from your personal repository. If any other branch or tag doesn’t reference those commits, they may eventually be cleaned up by Git’s garbage collection.
  • Deleting a remote branch removes the branch from the shared repository (e.g., GitHub, GitLab, Bitbucket), making it inaccessible to collaborators. However, any local copies of that branch on other machines will remain until they are manually removed or updated.

Why delete a branch?

Deleting a branch is good housekeeping, plain and simple. In the same way it’s good practice to delete duplicate photos, old downloads, and outdated folders, there are several good reasons to delete a branch once you’re done with it:

  • It keeps your repository clean. Too many branches, especially outdated ones, make it harder to track active work.
  • It prevents confusion. If a branch is no longer relevant, keeping it around can cause uncertainty about whether it’s still in use.
  • It improves collaboration. In team environments, deleting completed feature branches signals that work is done and prevents unnecessary work on outdated code.
  • It reduces remote repository bloat. Remote repositories can become cluttered with stale branches, making it harder for you (or a viewer) to navigate the project history.

If you’re new to Git or need to set it up before managing branches, check out the Git installation tutorial for step-by-step instructions.

Learn Git Fundamentals Today

For beginners: Master version control using Git.
Start Learning For Free

Deleting A Local Git Branch

Deleting a local branch is not difficult; luckily, it’s one of the more straightforward Git tasks. Let’s review the safest way to do it and what to watch out for.

The basic command to delete a local branch

The safest way to delete a local branch is with the -d flag:

git branch -d <branch_name>

Keep in mind that this command only works if the branch has been fully merged into the current branch (typically main or master). If there are any unmerged changes, Git will stop you to prevent accidental data loss.

Force deleting a local branch

If you try to delete a branch that still has unmerged changes, Git won’t let you unless you force it:

git branch -D <branch_name>

The -D flag (notice the uppercase) skips the safety check and deletes the branch immediately, whether it’s merged or not. I would highly advise exercising caution with this. Any unmerged work will be lost unless you have a backup or another branch pointing to the same commits.

Checking for unmerged changes before deletion

Before you arrive at the point of deleting a branch, it’s a good idea to check if it has unmerged changes. You can do this with:

git branch --no-merged

The above command lists all branches that haven’t been merged into the current branch. If the one you’re about to delete appears in this list, double-check that you don’t need any of its changes before proceeding.

Want a quick reference for Git commands, including deleting branches? Download the handy Git Cheat Sheet.

Deleting A Remote Git Branch

Deleting a remote branch is a bit different from deleting a local one. Since remote branches exist in shared repositories, removing them helps keep things organized and prevents outdated branches from cluttering up team workflows.

The basic command to delete a remote branch

To delete a branch from a remote repository like GitHub, GitLab, or Bitbucket, use:

git push origin --delete <branch_name>

This command removes the branch reference from the remote repository, making it inaccessible to others. However, any local copies of the branch on other machines won’t be affected they’ll need to be cleaned up separately.

Verifying remote branch deletion

After deleting a remote branch, you’ll need to confirm that it’s actually gone. First, fetch the latest updates from the remote repository:

git fetch --prune

This removes outdated remote-tracking references. To double-check that the branch is no longer listed, run the following:

git branch -r

The above command displays all remaining remote branches. If the deleted branch still shows up, try running another git fetch --prune or ensure the deletion command was executed on the correct remote.

Best Practices For Deleting Git Branches

Deleting branches is a routine part of working with Git, but doing it the right way can save you from unnecessary headaches. Here are some best practices to keep your repository organized and avoid potential mistakes.

Avoid deleting unmerged branches prematurely

Before deleting a branch, make sure it doesn’t contain any unmerged work that’s still needed. If you're unsure, check its status with:

git branch --no-merged

If the branch has valuable changes that haven't been merged, consider merging or archiving them first instead of deleting them outright.

Delete after merging

Once a feature or fix has been merged into the main branch (main or master), there’s no reason to keep the old branch around. Deleting merged branches helps prevent clutter and makes it easier to see which branches are still active.

Communicate with your team

As a professional courtesy, when you’re working on a collaborative project, avoid deleting a branch without warning your team. It can cause confusion—or worse, disrupt someone else’s work. Before removing a remote branch, check with your team to ensure no one is still using it. I prefer a simple Slack message or note to prevent frustration.

Before deleting a branch, you might want to tidy up your commit history by squashing commits. Learn how in our Git Squash Commits tutorial.

Common Issues When Deleting Git Branches

Deleting branches in Git is usually problem-free, but there are a few common pitfalls that can trip you up. Here’s what to watch out for and how to handle these issues should they come up.

Deleting a branch that’s not fully merged

Again, if you try to delete a branch that still has unmerged changes using git branch -d, Git will stop you to prevent potential data loss. This is a fail-safe. Any unmerged work will be lost if you force delete it with -D.

If you’ve already deleted a branch and realize you need it back, you may be able to recover it using Git’s reflog:

git reflog
git checkout -b <branch_name> <commit_hash>

This lets you restore the branch from its last known commit if it hasn’t been completely lost to garbage collection.

Deleting the current branch

Git won’t let you delete the branch you’re currently on, which makes sense otherwise, you’d be left without an active branch. If you see an error when trying to delete a branch, switch to another branch first:

git checkout main

Or, if using Git 2.23+:

git switch main

Once you're on a different branch, you can safely delete the one you intended to remove.

Need to switch between branches before deleting one? Our guide on checking out a remote branch in Git walks you through the process.

Accidentally deleting a remote branch

If a remote branch was deleted by mistake, you can restore it (assuming you still have a local copy) by pushing it back to the remote:

git push origin <deleted_branch_name>

This recreates the branch on the remote repository, restoring collaborators' access. If no local copy exists, you may need to manually check your commit history and recreate the branch.

Why Can't I Delete A Git Branch?

If you're having trouble deleting a branch in Git, revisit the common issues; check if it’s fully merged, you’re on the branch currently, or it’s a remote branch. From here, it may be your settings or permissions.  

  1. The first thing to check is whether you're currently on that branch. Git won’t let you delete a branch you’re actively working on, so you’ll need to switch to a different branch first. 
  2. If that’s not the issue, the branch might still have unmerged changes. Git protects branches with unmerged work, so unless you're absolutely sure you don’t need those changes, you’ll need to merge or force-delete the branch using git branch -D branch-name.
  3. If you’re trying to delete a remote branch, a simple git branch -d won’t do the trick. Instead, you’ll need to run git push origin --delete branch-name, and to clean up old references, you can use git fetch --prune
  4. Some repositories, especially those hosted on platforms like GitHub or GitLab, have protected branches that can't be deleted without changing the settings. If you're working in a shared repository, you may also need the right permissions to delete branches.

Still stuck? Running git branch -v can give you more details on the branch’s status and help you figure out what’s going on before trying again.

Automating Branch Cleanup With Git

I love good automation. While manually deleting branches works fine, and you should understand how to do it, there is another way. If you’re dealing with a lot of branches (like in a team setting), automation can save time and keep things tidier. Git offers a few ways to streamline branch cleanup, reducing clutter without extra effort.

Automate remote branch cleanup

When a remote branch is deleted, your local Git still holds onto a reference to it until you update things manually. Instead of cleaning them up one by one, you can use:

git fetch --prune

The above command automatically removes local references to branches that no longer exist on the remote repository, keeping everything in sync. Running this regularly prevents outdated branches from piling up in your local repo.

Use Git hooks for cleanup

For teams or larger projects, Git hooks can help automate branch cleanup. Hooks are custom scripts that run at certain points in the Git workflow, like after merging a branch. You can configure a post-merge hook to automatically delete merged branches after a set period, ensuring old branches don’t linger unnecessarily.

Conclusion

Branches can get messy over time, but a bit of “trimming” helps keep your repository clean and your workflow efficient. By making a habit of deleting merged branches, double-checking unmerged ones and keeping communication open in team projects, you can avoid these issues. And if you want to take it a step further, automation tools like git fetch --prune and Git hooks can help keep your repo clean without extra effort.

Deleting branches is just one step toward mastering Git. To deepen your understanding of version control, check out the Intermediate Git course. If you're working with GitHub repositories, a solid grasp of GitHub’s core concepts is essential. Learn more in the Introduction to GitHub Concepts course!

Learn Git Fundamentals Today

For beginners: Master version control using Git.

FAQs

Can deleting a branch in Git affect my production code?

No, deleting a branch does not affect production code as long as the branch has already been merged into your main branch (main or master). However, if you delete a branch with unmerged changes, any work that hasn’t been incorporated into another branch will be lost. Always check merge status before deleting.

How do I prevent my team from accidentally deleting important branches?

If you’re working in a shared repository (GitHub, GitLab, etc.), you can protect critical branches like main and develop by setting branch protection rules. This helps prevent accidental deletions and unauthorized force pushes. For local repositories, consider using naming conventions (e.g., prefixing active branches with wip- for work in progress) to indicate which ones shouldn’t be removed yet.

Is there a way to track who deleted a remote Git branch?

Yes, in repositories hosted on GitHub, GitLab, or Bitbucket, branch deletions are usually logged in the activity feed. You can check your repository’s audit logs or commit history to see who deleted a branch and when. For self-hosted Git repositories, you might need to set up server-side hooks to track deletions manually.

How do I recover a deleted Git branch if I closed my terminal?

If you deleted a branch and haven’t closed your terminal, you can undo it with:

git checkout -b <branch_name> <commit_hash>

But if you already closed your terminal, the best option is to use git reflog.

This will show a history of recent actions, including branch deletions. Find the last commit hash from the deleted branch and use it to recreate the branch. However, if the branch was deleted remotely and locally, and no one else has a copy, recovery might not be possible.

What’s the difference between `git branch -d` and `git branch -D` in real-world use?

  • git branch -d <branch> — Safe delete. It will prevent deletion if the branch has unmerged changes, acting as a safety net.
  • git branch -D <branch> — Force delete. It doesn’t care if the branch has unmerged changes — it will remove it immediately.

In a real-world scenario, you’d use -d most of the time to avoid losing work by accident. Use -D only when you’re 100% sure that the branch’s contents are no longer needed.


Ashlyn Brooks's photo
Author
Ashlyn Brooks

Ashlyn Brooks is a Technical Writer specializing in cloud platforms (Azure, AWS), API documentation, and data infrastructure. With over seven years of experience, she excels at translating complex concepts into clear, user-friendly content that enhances developer experience and product adoption.

Topics

Learn more about Git with these courses!

course

Introduction to Git

2 hr
16.3K
Discover the fundamentals of Git for version control in your software and data projects.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

tutorial

Git Rename Branch: How to Rename Local or Remote Branch

Learn how to rename local and remote Git branches using either the terminal or the graphical user interface (GUI) of popular clients like GitHub.
François Aubry's photo

François Aubry

5 min

tutorial

Git Checkout Remote Branch: Step-by-Step Guide

To git checkout a remote branch, you first need to fetch the latest changes from the remote repository, then you can checkout the remote branch locally using its full name (e.g., origin/branch-name).
François Aubry's photo

François Aubry

4 min

tutorial

Git Pull Force: How to Overwrite a Local Branch With Remote

Learn why git pull --force isn’t the best way to overwrite a local branch with the remote version, and discover the proper method using git fetch and git reset.
François Aubry's photo

François Aubry

tutorial

Git Switch Branch: A Guide With Practical Examples

Learn how to switch a branch in Git using git switch and understand the differences between git switch and git checkout.
François Aubry's photo

François Aubry

8 min

tutorial

Git Pull: Keeping Your Local Repository Up to Date

The git pull command retrieves updates from a remote repository and merges them into your current branch. Keep reading to learn to avoid unnecessary merge conflicts.
Oluseye Jeremiah's photo

Oluseye Jeremiah

8 min

tutorial

Git Prune: What Is Git Pruning and How to Use Git Prune

Git prune is a Git command that removes objects from the repository that are no longer reachable from any commit or branch, helping to free up disk space.
François Aubry's photo

François Aubry

5 min

See MoreSee More