Git Error: Cannot Delete Branch Checked Out or Used by Worktree

Tested on RHEL 10.2 (Coughlan)
Package git 2.52.0
Applies to Any host with Git installed
Privilege Normal user
Scope Fix branch deletion blocked by an active checkout or worktree: read git branch and git worktree list, switch away or free the linked worktree, then delete locally. Worked examples state a starting situation and commands — not a from-scratch lab.
Related guides Git branch examples
Delete Git branches
Git switch command
Git rebase tutorial
Git merge examples

Git refuses git branch -d when the branch is still checked out somewhere. Run git branch and git worktree list first — they show whether the block is your current checkout or a linked worktree path.

The sections below are independent scenarios. Pick the one that matches your error message and command output — they are not steps in one lab sequence.


Check What's Blocking Branch Deletion

When to use this: you just ran git branch -d and Git printed a checkout or worktree error.

List local branches and read the markers Git prints beside each name:

bash
git branch
output
* main
+ feature2
  demo

Git documents two checkout markers:

Marker Meaning
* Checked out in your current directory
+ Checked out in another linked worktree

You cannot delete feature2 while the + shows it is still active elsewhere. You cannot delete a branch marked with * until you switch away in this directory.

List every working tree path that holds a checkout:

bash
git worktree list
output
/path/to/repo        cc1f6a7 [main]
/path/to/feature-wt  cc1f6a7 [feature2]

Each path with a branch name in brackets is blocking deletion of that branch until that worktree switches away or is removed.

Situation What to do
* on the branch in git branch git switch <other-branch>, then git branch -d <name>
+ on the branch, or branch listed at another path Switch that worktree away, or remove it if unused — see below
Worktree folder deleted manually but error remains git worktree prune, then retry delete
Worktree folder moved manually git worktree repair <new-path>, then switch away or remove
git switch blocked by unfinished operation Finish or abort the operation, then switch away

The sections below walk through the checkout and worktree cases in detail.


What "Cannot Delete Branch Checked Out" Means

Git does not allow a branch reference to be deleted while a working tree is actively using that branch. You must first switch that worktree to another branch, detach it, or remove the linked worktree.

You may see either phrasing:

Error message Typical cause
cannot delete branch … checked out at … Older Git wording for an active checkout
cannot delete branch … used by worktree at … Current Git wording — includes the main repo path, not only extra worktrees

On Git 2.52, deleting the branch you are on in the main repository often prints the worktree form:

bash
git branch -d feature
output
error: cannot delete branch 'feature' used by worktree at '/path/to/repo'

The fix is the same: free every checkout that still uses the branch, then delete it.


Switch Away, Then Delete the Branch

Scenario: git branch shows * on the branch you want to remove, and no + marker or second path in git worktree list holds that branch.

Move HEAD to another local branch such as main:

bash
git switch main

Confirm the asterisk moved:

bash
git branch
output
* main
  feature

The feature branch is no longer checked out in this directory, so Git can delete it:

bash
git branch -d feature
output
Deleted branch feature (was 3fd1f94).

If Git instead reports the branch 'feature' is not fully merged, see the section on unmerged branches below — that is a different error after the checkout block is cleared.


Free the Branch Used by Another Worktree

Scenario: git branch shows + feature2, or git worktree list shows feature2 checked out at a path other than your current directory.

You can keep the linked worktree and switch it away, or remove the worktree when you no longer need it.

Keep the Worktree: Switch Away From the Branch

Change directory to the linked worktree and move it off the branch you want to delete:

bash
cd /path/to/feature-wt

Switch that worktree to another branch:

bash
git switch main

If you only need to free the branch name and do not need another branch checked out there, detach HEAD instead:

bash
git switch --detach

Return to the main repository and delete the branch:

bash
cd /path/to/repo
git branch -d feature2

Worktree No Longer Needed: Remove It

When you do not need the linked directory at all, remove the worktree from the main repository:

bash
git worktree remove /path/to/feature-wt

Then delete the branch:

bash
git branch -d feature2
output
Deleted branch feature2 (was cc1f6a7).

If git worktree remove refuses, Git is protecting a linked worktree that is not clean. By default, Git removes only a clean linked worktree — if it contains tracked modifications or untracked files, review, commit, or stash anything you need before removing it.


If the Worktree Folder Was Already Deleted

Scenario: someone removed the worktree directory from disk without running git worktree remove, and Git still blocks deletion.

Use git worktree prune only when the linked worktree is genuinely gone and you no longer need that checkout path. Pruning drops orphaned metadata — it does not reconnect a directory you moved elsewhere.

If the worktree was moved rather than deleted, use git worktree repair with the new path instead of pruning it:

bash
git worktree repair /path/to/new/feature-wt

When the directory was deleted and you do not need it back, prune orphaned metadata:

bash
git worktree prune

Then retry git branch -d.


If Git Won't Let You Switch Away

Scenario: git switch fails because Git still considers the branch involved in an unfinished operation — often a merge, rebase, or bisect that prevents leaving the current checkout.

Read what Git reports:

bash
git status

If the status line names an unfinished operation, complete or abort it before switching branches. To cancel the most common blockers:

Operation Cancel
Merge git merge --abort
Rebase git rebase --abort
Cherry-pick git cherry-pick --abort
Bisect git bisect reset

After the operation completes or aborts, use git switch to move off the branch and delete it.


When git branch -d Says Not Fully Merged

Scenario: you switched away successfully, but delete still fails with a merge warning — not a checkout error.

bash
git branch -d feature
output
error: the branch 'feature' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature'

git branch -d only deletes branches whose commits are reachable from your current branch. To discard the branch anyway:

bash
git branch -D feature
output
Deleted branch feature (was 3fd1f94).

Use -D only when you are sure those commits are not needed. -D forces deletion when Git considers the branch unmerged — it does not bypass an active checkout. You still must switch every worktree away or remove the linked worktree first.


Troubleshooting

Problem What to do
used by worktree at points at the main repo path Switch away with git switch <other-branch>, then delete
used by worktree at points at a second directory Switch that worktree away, or git worktree remove <path> if unused, then delete
git worktree remove refuses because of local changes Review, commit, or stash tracked changes and untracked files in that worktree before removing it
Worktree path no longer exists on disk git worktree prune, then retry delete
Worktree directory was moved, not deleted git worktree repair <new-path> — do not prune if you still need the checkout
git status says you are currently bisecting Finish the bisect or run git bisect reset, then retry branch deletion
not fully merged after checkout is cleared Use git branch -D only if you accept losing unmerged commits
Need to delete a remote branch That is a separate git push workflow — see delete Git branches

References

Git documentation: git-branch
Git documentation: git-worktree
Git documentation: git-bisect


Summary

When git branch -d fails with cannot delete branch checked out at or cannot delete branch used by worktree, a working tree still has that branch checked out. Run git branch and read * for your current directory and + for another linked worktree, then confirm paths with git worktree list.

If the branch is current here, git switch away and delete again. If another worktree holds the checkout, either cd into that path and switch it to another branch or --detach, or remove the worktree when you no longer need it. Use git worktree prune when the directory was deleted manually, or git worktree repair <new-path> when it was moved.

When git switch itself is blocked, read git status and abort the unfinished operation with the matching command — merge, rebase, cherry-pick, or bisect — before switching. Once every checkout is freed, git branch -d deletes merged branches. A follow-up not fully merged message is a separate check: use git branch -D only when you accept losing unmerged commits, knowing that -D still cannot delete a branch that remains checked out.


Frequently Asked Questions

1. Why does Git say cannot delete branch checked out?

Git blocks deleting a branch that is still checked out in a working tree. Switch that worktree to another branch, detach it, or remove the linked worktree, then run git branch -d.

2. What does cannot delete branch used by worktree mean?

The branch is still attached to a working tree path listed by git worktree list. That includes the main repository directory on recent Git versions, or a separate linked worktree directory.

3. How do I delete a branch used by worktree?

Run git worktree list. Either cd into the linked worktree and git switch to another branch, or remove the worktree with git worktree remove when you no longer need it, then delete with git branch -d.

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

git branch -d deletes only when the branch is fully merged into your current branch. git branch -D force-deletes even when commits are unmerged.

5. Can I force delete a checked out branch?

No. You must switch that worktree to another branch, detach it, or remove the linked worktree first. Force delete with -D does not bypass an active checkout.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)