In a typical Git repository you may have a couple of permanent branches, such as `master` and `development` that are constantly worked on, but also a number of branches that are temporary, for things like bug fixes, new features, or versions of your project.

Lots of developers choose to delete their branches once they've finished working on them to keep their repository tidy. Cleaning up merged feature branches prevents clutter, reduces confusion when listing branches, and makes it easier for your team to see which work is still active. If a branch has been merged into `master` or `main`, there is no reason to keep it around.

There are two steps to deleting a branch from a repository completely:

1. [Delete the branch locally](#deleting-a-local-branch)
2. [Delete the branch on the remote](#deleting-a-remote-branch)

## Deleting a local branch

You can use [`git branch`](/git/branching-and-merging) (with no arguments) for a list of all the local branches you have "checked out" on your computer.

```bash
* master
  your-branch-name
```

Run the following command to delete a specific branch locally:

```bash
$ git branch -d your-branch-name
```

The `-d` flag is the safe option. It only deletes branches that have already been fully merged into their upstream branch or into `HEAD`. If the branch contains work that hasn't been merged, Git refuses and shows an error:

```
error: The branch 'your-branch-name' is not fully merged.
If you are sure you want to delete it, run 'git branch -D your-branch-name'.
```

To force-delete an unmerged branch, use the uppercase `-D` flag:

```bash
$ git branch -D your-branch-name
```

Use `-D` with caution — any commits on that branch that haven't been merged or cherry-picked elsewhere will become harder to recover.

One more thing to note: you cannot delete the branch you currently have checked out. Switch to another branch first (for example, `git checkout master`) before running the delete command.

For more on how branches work in Git, see our guide on [branching and merging](/git/branching-and-merging).

## Deleting a remote branch

If you've previously pushed the branch, it'll still exist on the remote repository. You can see this by running `git branch -r` to get a list of all the remote branches.

```bash
origin/master
origin/your-branch-name
```

In order to delete the branch remotely, you'll need to "push" the deletion using [`git push`](/git/publishing-local-changes) by running the following command:

```bash
git push --delete origin your-branch-name
```

You may also see an older alternative syntax that does the same thing:

```bash
git push origin :your-branch-name
```

The colon prefix tells Git to push "nothing" into the remote branch, which deletes it.

After deleting a remote branch, other collaborators who have fetched that branch will still see a stale tracking reference on their machines. They should run [`git fetch --prune`](/git/commands/git-fetch) to clean up those references.

## Cleaning up stale branches

Over time, remote branches get deleted but your local tracking references stick around. To see which local branches have already been merged and are safe to remove:

```bash
$ git branch --merged
```

Any branch listed (other than the one marked with `*`) can safely be deleted with `git branch -d`.

To remove stale remote tracking references that point to branches that no longer exist on the remote, run:

```bash
$ git fetch --prune
```

This is generally preferred over `git remote prune origin` because it also fetches the latest changes at the same time. For a deeper look at fetch, see our [git fetch](/git/commands/git-fetch) reference.

You can also [clean up old remote branches](/git/faqs/how-to-clean-up-old-remote-git-branches) in bulk if your repository has accumulated many stale branches.

## Protecting important branches

Most hosting platforms like GitHub and GitLab let you set up branch protection rules to prevent accidental deletion of critical branches such as `main` or `develop`. In GitHub, go to your repository's **Settings > Branches > Branch protection rules** and enable "Prevent deletions" for the branches you want to protect. GitLab offers similar controls under **Settings > Repository > Protected branches**.

This is especially useful on shared repositories where a team member could accidentally run `git push --delete origin main` and disrupt the entire workflow.

If you accidentally delete a branch and need it back, see [how to recover a deleted branch](/git/faqs/recover-deleted-branch-git).

Ready to streamline your Git workflow? [DeployHQ](https://www.deployhq.com) deploys your code automatically whenever you push to your repository.