Gitでcannot delete branchと出たときの直し方
git branch -d や git branch -D で error: cannot delete branch 'ブランチ名' used by worktree at 'パス' と出たら、そのブランチはどこかの作業フォルダーで使われています。使っている場所でほかのブランチに移るか、その作業フォルダーを片付ければ、同じコマンドで消せるようになります。
マージが終わって要らなくなったブランチを片付けるときに出ることがあるエラーです。使っている場所には、いまのフォルダー、git worktree で作った別のフォルダー、すでに消した worktree のフォルダー、rebase や bisect の途中の4つの場合があり、それぞれ直し方が違います。
検証環境は Ubuntu 24.04、Git 2.43.0、bash 5.2 です。英語のメッセージにそろえるため LANG=C で実行しました。出力に出る作業フォルダーのパスは /path/to/… に置き換えています。Windows や macOS、Git の GUI ツールでの表示は試していません。

いまいるブランチは -D でも消せない
feature/login にいるまま、そのブランチを消そうとしました。-d でも、強制的に消す -D でも、同じメッセージで止まりました。終了コードは 1 です。
$ git branch
* feature/login
main
$ git branch -d feature/login
error: cannot delete branch 'feature/login' used by worktree at '/path/to/app'
$ git branch -D feature/login
error: cannot delete branch 'feature/login' used by worktree at '/path/to/app'
at の後ろに出ているのは、いま作業しているフォルダー(/path/to/app)です。git branch の一覧で * が付いているブランチが、いまいるブランチです。
git branch の公式リファレンスによると、-D は --delete --force の短い書き方です。-d と組み合わせた --force は、マージ済みかどうかに関係なく消すためのものです。使用中かどうかの確認は、--force を付けても変わりません。Git のソース(builtin/branch.c)を見ると、ブランチを消す前に、どこかの作業フォルダーで使われていないかを確かめ、使われていればこのエラーを出して、そのブランチは消さずに終えています。
直し方:ほかのブランチに移ってから消す
main に移ってから、同じコマンドを実行すると消えました。
$ git switch main
Switched to branch 'main'
$ git branch -d feature/login
Deleted branch feature/login (was ddb34d1).
$ git branch
* main
ほかの worktree で開いているブランチ
git worktree add で作った別のフォルダーでブランチを開いていると、いまのフォルダーでは別のブランチにいても消せません。このときは、at の後ろに、そのフォルダーのパスが出ます。
$ git worktree add ../app-hotfix hotfix
Preparing worktree (checking out 'hotfix')
HEAD is now at ddb34d1 ログイン画面
$ git branch
+ hotfix
* main
$ git branch -d hotfix
error: cannot delete branch 'hotfix' used by worktree at '/path/to/app-hotfix'
git branch の一覧で + が付いているのが、ほかの作業フォルダーで開いているブランチです。どのフォルダーでどのブランチを開いているかは、git worktree list で一覧にできます。
$ git worktree list
/path/to/app ddb34d1 [main]
/path/to/app-hotfix ddb34d1 [hotfix]
worktree の使い方そのものは、別の記事「git worktreeで、別のブランチを別のフォルダーで同時に開く方法」で扱っています。
直し方1:そのフォルダーで、ブランチから離れる
そのフォルダーをまだ使うなら、そのフォルダーでほかのブランチに移ります。移る先のブランチがないときは、git switch に --detach を付けると、ブランチではなくコミットを開いた状態(detached HEAD)にできます。-C でフォルダーを指定すれば、cd で移らずに実行できます。
$ git -C ../app-hotfix switch --detach
HEAD is now at ddb34d1 ログイン画面
$ git branch -d hotfix
Deleted branch hotfix (was ddb34d1).
$ git worktree list
/path/to/app ddb34d1 [main]
/path/to/app-hotfix ddb34d1 (detached HEAD)
直し方2:フォルダーごと片付ける
フォルダーも要らないなら、git worktree remove で片付けてから消します。片付けたあとは、同じ git branch -d で消えました。
$ git branch -d hotfix2
error: cannot delete branch 'hotfix2' used by worktree at '/path/to/app-hotfix2'
$ git worktree remove ../app-hotfix2
$ git branch -d hotfix2
Deleted branch hotfix2 (was ddb34d1).
git worktree の公式リファレンスによると、git worktree remove は、変更したファイルや Git で管理していないファイルが残っているフォルダーは消しません。--force を付けると、そのようなフォルダーも消せます。
フォルダーを消したのに、まだ使われていると出る
worktree のフォルダーを rm -rf などで直接消すと、Git の側には、そのフォルダーでブランチを開いているという記録が残ります。この状態でも、ブランチは消せませんでした。at の後ろには、もうないフォルダーのパスが出ます。
$ rm -rf ../app-exp
$ git worktree list
/path/to/app ddb34d1 [main]
/path/to/app-exp ddb34d1 [experiment] prunable
$ git branch -d experiment
error: cannot delete branch 'experiment' used by worktree at '/path/to/app-exp'
git worktree list で prunable と出ているのが、フォルダーが見つからない worktree です。git worktree の公式リファレンスによると、git worktree prune で片付けられるものに prunable と表示されます。
--dry-run を付けると、何も消さずに、消すものだけを表示します。中身を確かめてから prune を実行すると、記録が消え、ブランチも消せるようになりました。
$ git worktree prune --dry-run --verbose
Removing worktrees/app-exp: gitdir file points to non-existent location
$ git worktree prune
$ git worktree list
/path/to/app ddb34d1 [main]
$ git branch -d experiment
Deleted branch experiment (was ddb34d1).
rebase や bisect の途中のブランチ
rebase がコンフリクトで止まっている間は、git branch --show-current は何も表示しません。いまいるブランチがないように見えますが、rebase しているブランチを消そうとすると、いまのフォルダーで使われているとして止まりました。
$ git rebase main topic
Rebasing (1/1)
Auto-merging a.txt
CONFLICT (add/add): Merge conflict in a.txt
error: could not apply ecd5e23... topic のコミット
(hint: の行と、最後の Could not apply の行は省略)
$ git branch --show-current
$ git branch
* (no branch, rebasing topic)
main
topic
$ git branch -D topic
error: cannot delete branch 'topic' used by worktree at '/path/to/app'
git branch の一覧の先頭に (no branch, rebasing topic) と出ていて、topic を rebase している途中だと分かります。Git のソース(branch.c)でも、各作業フォルダーが rebase か bisect の途中なら、その対象のブランチを使用中の一覧に入れています。
git rebase の --abort は、rebase をやめて元のブランチに戻します。実行すると topic に戻ったので、main に移ってから消しました。
$ git rebase --abort
$ git branch --show-current
topic
$ git switch main
Switched to branch 'main'
$ git branch -D topic
Deleted branch topic (was ecd5e23).
git bisect の途中も同じでした。一覧に (no branch, bisect started on search) と出ているときに search を消そうとすると、同じエラーになりました。git bisect の公式リファレンスのとおり、git bisect reset で bisect を終えると元のブランチに戻るので、ほかのブランチに移ってから消せます。
$ git branch
* (no branch, bisect started on search)
main
search
$ git branch -D search
error: cannot delete branch 'search' used by worktree at '/path/to/app'
$ git bisect reset
Previous HEAD position was 022b175 search 2
Switched to branch 'search'
$ git switch main
Switched to branch 'main'
$ git branch -D search
Deleted branch search (was 3023756).
not fully merged と出たときは別の原因
ブランチを消すときに出るエラーには、not fully merged もあります。こちらはブランチを使っているからではなく、まだマージしていないコミットがあるため、-d が消すのを止めています。
$ git branch -d draft
error: the branch 'draft' is not fully merged.
If you are sure you want to delete it, run 'git branch -D draft'
$ git branch -D draft
Deleted branch draft (was 611eca4).
メッセージのとおり -D にすると消えました。draft にしかないコミットがあるので、-D を使う前に、そのコミットが要らないかどうかを確かめてください。-d と -D の違いや、消したブランチの作り直し方は、別の記事「Gitでブランチの名前を変える方法と消す方法」で扱っています。
cannot delete branch は、-D に変えても直りません。ブランチを使っている場所でほかのブランチに移るか、作業フォルダーやその記録を片付けてから消します。




