Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to Delete a Branch in Git

Updated November 30, 2026 · Written and reviewed under our editorial policy

To delete a local Git branch, switch to another branch and run git branch -d branch-name. Git refuses if the branch isn't merged; use git branch -D branch-name to force it. To delete the branch on the remote, run git push origin --delete branch-name.

To delete a branch locally and on the remote:

  • Switch away from the branch: git switch main.
  • Delete it locally: git branch -d branch-name.
  • If Git says it isn't fully merged and you're sure, run git branch -D branch-name.
  • Delete it on the remote: git push origin --delete branch-name.
  • Clean up stale references: git fetch --prune.

Source: Git documentation, "git-branch", "git-push" and "git-fetch" (git-scm.com/docs), checked 2026-11-28.

These commands work in any terminal once you install Git. Replace branch-name with your branch and main with your default branch if it has another name.

Delete a local branch

Deleting a local branch in Git removes the branch from your copy of the repository; the branch on the remote, if there is one, stays. Use -d for a branch whose work is merged:

git switch main
git branch -d feature-x

git branch -d deletes the branch only if it's fully merged into its upstream branch or, if it has none, into the branch you're on. Otherwise Git stops with:

error: the branch 'feature-x' is not fully merged

If you're sure you don't need those commits, force it with git branch -D feature-x (short for --delete --force). You can delete several branches in one command: git branch -d feature-x feature-y.

Older Git versions don't have git switch; install the latest Git to get it. Until you do, git checkout main does the same job here. Starting fresh? Copy a repository with git clone, then create or delete branches in your copy.

Before you delete a feature branch, make sure it merged clean. Kane CLI can run end-to-end checks on any branch from your terminal, so you can test every branch before you merge.

Command Prompt showing git branch -d failing with not fully merged, then git branch -D deleting the branch

Delete a remote branch

Deleting a remote branch in Git takes a push, because the branch lives on the server:

git push origin --delete feature-x

The older form, git push origin :feature-x, does the same thing: it pushes nothing into the remote branch, which deletes it. You need push access, and hosts such as GitHub and GitLab refuse to delete the default branch or a protected branch.

Deleting the remote branch doesn't delete your local one; run git branch -d for that. Other people's clones also keep a stale origin/feature-x reference until they prune it.

Command Prompt showing git push origin --delete removing a remote branch

Fix "Cannot delete branch checked out"

The "Cannot delete branch" error means Git won't delete a branch that is checked out. Depending on your Git version, the message reads:

error: Cannot delete branch 'feature-x' checked out at '/path/to/repo'
error: cannot delete branch 'feature-x' used by worktree at '/path/to/repo'

Switch to another branch first with git switch main, then delete it. If the path in the message is a different folder, the branch is checked out in another worktree: switch branches in that folder, or remove the worktree with git worktree remove <path>.

Delete all merged branches

Deleting all merged branches in Git is safest in two passes: list them, check the list, then delete. First, see which local branches are merged into main:

git switch main
git branch --merged main

Read the list. The line starting with * is the branch you're on, and long-lived branches such as main, master or develop should stay. When the list looks right, delete the rest:

git branch --merged main | grep -vE '^[*+]|^[[:space:]]*(main|master|develop)$' | xargs git branch -d

The grep skips the current branch, branches checked out in other worktrees (marked +) and the branch names in parentheses; edit that list to match your repository. Because the command uses -d, Git still refuses any branch that isn't merged. On Windows, run it in Git Bash.

Prune remote-tracking branches

Pruning removes remote-tracking branches, the origin/... references in your clone, whose branch no longer exists on the remote. Run:

git fetch --prune

git remote prune origin removes the same stale references without fetching new commits. To prune on every fetch, run git config --global fetch.prune true.

Pruning never deletes your local branches. To find local branches whose remote branch is gone, run git branch -vv and look for : gone] next to the branch name. A fresh copy has no stale references at all: if you clone a Git repository again, you get only the branches that exist on the remote now.

Restore a deleted branch with git reflog

Restoring a deleted Git branch works as long as its commits still exist in your repository. When you delete a branch, Git prints its last commit:

Deleted branch feature-x (was 1a2b3c4).

Recreate the branch from that commit with git branch feature-x 1a2b3c4. If the message is gone, run git reflog to list where HEAD has pointed recently, find the last commit you made on the branch, and use its hash in the same command. Git removes unreachable commits during garbage collection after a while, so do this soon.

For a branch deleted on GitHub after a pull request, the pull request page has a Restore branch button. Reflog, pruning and force-deletes come up often in our Git interview questions.

Delete a branch on GitHub or GitLab

Deleting a branch on GitHub takes two steps in the browser:

  • Open the repository and click Branches (or open the branch menu and click View all branches).
  • Find the branch and click its trash can icon (Delete branch).

After a pull request merges, GitHub also shows a Delete branch button on the pull request, and the repository setting Automatically delete head branches does it for you. On GitLab, go to Code > Branches and click the delete icon next to the branch; Delete merged branches on the same page removes every merged branch except protected ones.

Deleting a branch in the browser doesn't touch your computer: run git branch -d locally, then git fetch --prune. If you run tests in GitHub Actions, check that the pull request's checks passed before you delete its branch.

Frequently Asked Questions

What is the difference between git branch -d and -D?

git branch -d deletes a branch only if its work is merged into its upstream branch or, if it has none, into the branch you're on. git branch -D, short for --delete --force, deletes it even if its commits aren't merged anywhere.

How do I delete a remote branch?

Run git push origin --delete branch-name. Other clones keep a stale origin/branch-name reference until they run git fetch --prune.

Can I recover a deleted Git branch?

Yes, as long as its commits still exist in the repository. Take the commit hash Git printed when you deleted the branch, or find it with git reflog, and run git branch branch-name <hash>.

Why can't I delete my current branch?

Git won't delete a branch that is checked out. Switch to another branch with git switch main, then delete it. If the error names a different folder, the branch is checked out in another worktree.

Does deleting a local branch delete it on GitHub?

No. git branch -d only removes the branch from your computer. Delete the GitHub branch with git push origin --delete branch-name or from the repository's Branches page.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests