Git sometimes prevents branch deletion to protect the repository from losing active work or breaking an ongoing operation. The error typically appears when the branch is currently checked out, being used by a worktree, or involved in an operation like rebase or merge.
Understanding the root cause of the error helps you quickly apply the correct command and safely delete the branch without affecting repository integrity. If you're new to branch operations, first understand how branches work in git branch basics.
Git Cannot Delete Branch Error - Quick Fix
Switch to another branch before deleting
If you are currently on the branch you want to delete, Git will block the operation. Switch to another branch, and then delete the target branch.
git checkout main
git branch -d <branch-name>This ensures that the branch you want to delete is no longer the active working branch.
Remove worktree reference
If the branch is associated with a Git worktree, Git will prevent deletion until the worktree is removed.
git worktree list
git worktree remove <path-to-worktree>
git branch -d <branch-name>Removing the worktree frees the branch so it can be deleted safely.
Abort rebase or merge
If a rebase or merge is in progress, Git locks the branch to prevent inconsistent history. You may need to abort it:
git rebase --abort
git merge --abortOnce the operation is aborted or completed, switch branches and delete the target branch.
Force delete branch when safe
If the branch contains unmerged commits but you still want to delete it, you can force delete it using the -D option.
git branch -D <branch-name>Use this carefully because it permanently deletes the branch even if changes are not merged. If something goes wrong, you can recover using git reflog.
Git Branch Deletion Quick Reference Table
Safe branch deletion commands
| Task | Command |
|---|---|
| List local branches | git branch |
| Switch to another branch | git checkout main |
| Delete merged branch | git branch -d <branch-name> |
| Verify merged branches | git branch --merged |
Force deletion commands
| Task | Command |
|---|---|
| Force delete branch | git branch -D <branch-name> |
| Delete remote branch | git push origin --delete <branch-name> |
| Remove stale remote branches | git fetch --prune |
Worktree troubleshooting commands
| Task | Command |
|---|---|
| List worktrees | git worktree list |
| Remove worktree | git worktree remove <path> |
| Clean stale worktree references | git worktree prune |
Branch cleanup commands
| Task | Command |
|---|---|
| Show merged branches | git branch --merged |
| Show unmerged branches | git branch --no-merged |
| Delete merged branches automatically | git branch --merged | grep -v "\*" | xargs git branch -d |
Understanding the Git Error: Cannot Delete Branch
What does "Cannot delete branch checked out at" mean
The error "cannot delete branch checked out at" occurs when you attempt to delete a branch that is currently active in your working directory. Git prevents this action because deleting the active branch would leave the repository without a valid reference for the current working state.
For example, if you are currently working on a branch called feature-login and try to delete it, Git will block the operation to avoid losing uncommitted work or leaving the working directory in an inconsistent state.
What does "Cannot delete branch used by worktree" mean
The error "cannot delete branch used by worktree" appears when the branch you are trying to delete is attached to a Git worktree. A worktree allows multiple working directories to be connected to the same repository so that different branches can be worked on simultaneously.
If a branch is associated with an active worktree, Git will prevent deletion until that worktree is removed or detached. This safeguard ensures that files being actively used in another working directory are not unexpectedly removed.
When this error commonly occurs in Git workflows
This error typically occurs in the following situations:
- The branch you want to delete is currently checked out
- The branch is attached to an active Git worktree
- A rebase or merge operation is in progress
- The repository has stale worktree references
- The working directory has not refreshed its internal state
Understanding these scenarios helps you quickly determine the appropriate command to resolve the issue.
Branch is Currently Checked Out
Identify the active branch in Git
Before deleting a branch, first verify the branch is currently active in your repository.
git branchThe active branch will appear with an asterisk (*) next to it. For example:
* feature-login
main
developIn this case, feature-login is the currently checked-out branch.
Switch to another branch safely
To delete a branch, you must first
switch to another branch such as main or develop.
git checkout mainThis command changes the working directory to the main branch and frees the previously active branch so it can be deleted.
Delete the branch after switching
Once you are no longer on the branch, you can safely delete it.
git branch -d feature-loginThe -d option safely deletes the branch only if it has already been merged.
Branch Used by Git Worktree
What is Git worktree
Git worktree is a feature that allows multiple working directories to be attached to the same repository. This allows developers to work on different branches simultaneously without switching branches repeatedly.
For example, a repository might have one directory working on main and another directory working on a feature branch.
Why Git prevents deleting branches used by worktrees
If a branch is currently associated with a worktree, Git prevents deletion to avoid breaking that working directory. Removing the branch while it is actively used by a worktree could leave files in an inconsistent state.
Therefore, Git requires that the worktree be removed before deleting the branch.
Identify worktrees using git worktree list
To check whether a branch is connected to a worktree, run the following command:
git worktree listExample output:
/path/repo abc123 [main]
/path/repo-test def456 [feature-login]This output shows which branches are currently attached to different working directories.
Remove worktree before deleting branch
If a branch is linked to a worktree, remove that worktree first.
git worktree remove /path/repo-testAfter removing the worktree, you can delete the branch normally.
git branch -d feature-loginWorking Directory Not Updated
Refresh Git repository state
Sometimes Git may still believe that a branch is checked out due to stale repository state or incomplete updates. Running a simple status check refreshes the internal index.
git statusThis command synchronizes the working directory and helps Git recognize the current branch state.
Run git status to sync working directory
The git status command updates Git's view of the repository and ensures that branch references are accurate.
This step often resolves minor inconsistencies where Git still believes a branch is active even after switching branches.
Clean stale references using git worktree prune
If worktrees were deleted manually or moved, Git may still hold references to them. You can clean up these stale references using the following command:
git worktree pruneThis command removes outdated worktree metadata and allows you to safely delete the branch afterward.
Rebase or Merge is in Progress
Detect active rebase operations
If a rebase operation is currently running on a branch, Git locks the branch to maintain repository consistency. Attempting to delete the branch during a rebase will trigger an error.
You can check whether a rebase is in progress by running:
git statusIf a rebase is active, Git will display a message such as:
You are currently rebasing branch 'feature-login'This indicates that the rebase process must be completed or aborted before deleting the branch.
Continue Git rebase after resolving conflicts
During a rebase, conflicts may occur when Git tries to apply commits on top of another branch. After resolving these conflicts manually, you can continue the rebase process using:
git rebase --continueRepeat this step until all commits are successfully reapplied and the rebase completes.
Once the rebase is finished, you can switch to another branch and delete the target branch.
Abort Git rebase safely
If you decide not to proceed with the rebase or encounter complex conflicts, you can abort the operation and restore the branch to its previous state.
git rebase --abortThis command stops the rebase and returns the repository to the state it was in before the rebase began.
After aborting the rebase, you can switch branches and delete the branch safely.
Delete the branch after rebase completion
Once the rebase is completed or aborted, switch to another branch and remove the target branch.
git checkout main
git branch -d feature-loginGit will now allow the branch to be deleted because it is no longer involved in any ongoing operation.
Cannot Delete Remote Branch
Delete remote branch using git push origin --delete
If the branch exists on a remote repository such as GitHub or GitLab, you must delete it using a push command like git push.
git push origin --delete <branch-name>For example:
git push origin --delete feature-loginThis command instructs the remote repository to remove the specified branch.
Verify remote branch deletion
After deleting the remote branch, confirm that it no longer exists by listing remote branches.
git branch -rThis command displays all remote branches currently available in the repository.
Sync remote branch list using git fetch prune
Sometimes Git may still show deleted remote branches locally due to outdated references, which can be cleaned using git fetch.
git fetch --pruneThis command removes stale references and ensures your local repository reflects the current state of the remote repository.
Git Commands for Fixing Branch Deletion Errors
git branch -d vs git branch -D
Git provides two commands for deleting branches locally.
| Command | Behavior |
|---|---|
git branch -d <branch> | Safely deletes a branch only if it has been merged |
git branch -D <branch> | Force deletes a branch even if it has unmerged commits |
The -d option is recommended for normal use because it prevents accidental data loss.
git worktree prune explained
The git worktree prune command removes stale worktree metadata that may remain after a worktree directory is deleted manually.
git worktree pruneThis helps resolve errors where Git believes a branch is still associated with a worktree that no longer exists.
git worktree remove usage
If a branch is attached to an active worktree, you must remove the worktree before deleting the branch.
git worktree remove <path-to-worktree>This command safely removes the worktree directory and its associated administrative files.
git branch --merged command
The git branch --merged command lists all branches that have already been merged into the current branch.
git branch --mergedThis command helps identify branches that can be safely deleted without losing changes.
Common Mistakes When Deleting Git Branches
Deleting currently active branch
One of the most common mistakes is attempting to delete the branch that is currently checked out. Git blocks this action to prevent leaving the working directory without a valid branch context.
Always switch to another branch before deleting the target branch.
Deleting branch during rebase
Attempting to delete a branch during a rebase operation will trigger an error because Git temporarily reorganizes commits during the process.
Always complete or abort the rebase before performing branch deletion.
Ignoring worktree references
Branches connected to Git worktrees cannot be deleted until the worktree is removed. Failing to check worktree associations often leads to confusion when deletion commands fail.
Use git worktree list to verify if the branch is linked to another working directory.
Force deleting without checking merges
Using git branch -D without confirming whether the branch has been merged can result in permanent loss of commits.
Before force deleting a branch, verify its merge status using:
git branch --mergedThis ensures that important changes are not accidentally removed.
Frequently Asked Questions
1. Why does Git say cannot delete branch checked out?
Git prevents deleting a branch that is currently checked out because removing the active branch would leave the working directory without a valid branch reference.2. What does cannot delete branch used by worktree mean?
This error means the branch is currently associated with a Git worktree. You must remove the worktree or switch branches before deleting it.3. How do I delete a branch used by worktree?
First run git worktree list to identify the worktree, remove it using git worktree remove, and then delete the branch using git branch -d.4. What is the difference between git branch -d and -D?
git branch -d safely deletes a branch only if it has been merged, while git branch -D force deletes the branch even if it has unmerged commits.5. Can I force delete a checked out branch?
No. You must switch to another branch before deleting it. Git does not allow deleting the currently active branch.Summary
The Git error "cannot delete branch checked out at" or "cannot delete branch used by worktree" usually occurs when the branch you are trying to delete is still actively used by the repository. This can happen when the branch is currently checked out, attached to a worktree, involved in a rebase or merge operation, or when stale references remain in the repository.
To resolve this issue, you must first identify the underlying cause and apply the appropriate command. Common solutions include switching to another branch, removing associated worktrees, aborting or completing ongoing rebases, and cleaning up stale references using Git maintenance commands. Following safe branch management practices ensures that branches are deleted without disrupting ongoing development work. For complete steps, see delete Git branches safely.

![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch.webp)








