git worktree で作った作業ツリーを片付けるときに出るエラーを、メッセージごとに原因と直し方へ対応させた記事です。変更が残っていて remove できない、ブランチを git branch -d で消せない、ディレクトリを手で消したあとに残る prunable、ロックされた worktree を、小さなリポジトリで1つずつ起こして直しました。Windows だけで起きた「消したはずのフォルダが残る」場合も載せています。
結論
| 出たメッセージ | 原因 | 直し方 |
|---|---|---|
fatal: '../demo-feature' contains modified or untracked files, use --force to delete it | worktree に、コミットしていない変更か、追跡していないファイルがある | 残すならコミットしてから git worktree remove。捨てるなら git worktree remove --force |
error: cannot delete branch 'fix' used by worktree at '...' | そのブランチを、どこかの worktree がチェックアウトしている(-D でも同じエラー) | 先に git worktree remove で worktree を消してから、ブランチを消す |
error: the branch 'feature' is not fully merged | ブランチに、マージしていないコミットがある | 必要ならマージする。いらないなら git branch -D |
git worktree list に prunable が出る。fatal: '../demo-tmp' is a missing but already registered worktree; | worktree のディレクトリを手で消したので、リポジトリの中の管理情報だけが残っている | git worktree prune(1つだけなら git worktree remove <パス> でもよい) |
fatal: cannot remove a locked working tree, lock reason: ... | worktree がロックされている(--force を1回つけても消せない) | git worktree unlock してから remove。または git worktree remove -f -f |
(Windows)error: failed to delete '...': Permission denied、Invalid argument | ほかのプロセス(開いたままの端末、ファイルを開いているプログラム)や、Git を実行したシェル自体が、その worktree を使っている | 使っているプロセスを終わらせ、worktree の外に cd してから、残ったフォルダを手で消す |
あわせて気をつけたい点です。
.gitignoreで無視しているファイルは、removeのときに警告なしで消えました(--forceなしでも)。.envなどを worktree に置いているときは、先に退避してください。- Windows で削除に失敗したときは、Git の登録だけが先に消え、フォルダが残りました。そのあとは
removeもpruneも効かないので、フォルダを手で消します。
動作確認した環境
- Linux: Docker のコンテナ(Alpine Linux 3.24.4)で、Git 2.56.1(2026年11月16日の時点の最新の安定版)を公式のソースからビルドして使用
- Windows 11: Git for Windows 2.50.1(このPCに入っていた版)を、Git Bash と PowerShell 7.6.5 から実行
- Docker 29.7.2(Windows 11 の Docker Desktop、x86_64)
- 確認日: 2026年11月16日
この記事は、MacとWindowsの両方の読者を対象にしています。この記事のコマンドは、Claude(AI)が実行して確認しました。本文の出力は、Linux のコンテナで上から順に実行した結果です。同じ手順を Windows の Git Bash と PowerShell 7 でも実行し、エラーのメッセージは Linux と同じでした(パスの書き方は違います)。結果が違ったところは、最後の章「Windows で違ったところ」にまとめました。
Mac では確認していません。Mac のターミナル(zsh や bash)では、本文の Linux と同じコマンドを使う想定です。Windows で確かめた Git は 2.50.1 で、最新の Git for Windows(2.56.1.windows.1)では確かめていません。
準備
空のフォルダで、確認用のリポジトリ demo を作ります。worktree は、demo と同じ階層に ../demo-feature のような名前で作ります。.gitignore には、あとで使うために *.log を書いておきます。
$ git init -b main demo
Initialized empty Git repository in /work/wt-lab/demo/.git/
$ cd demo
$ git config user.name "Example User"
$ git config user.email "user@example.com"
$ echo hello > README.md
$ echo "*.log" > .gitignore
$ git add README.md .gitignore
$ git commit -q -m "first commit"
$ git worktree add ../demo-feature -b feature
Preparing worktree (new branch 'feature')
HEAD is now at 9b54f98 first commit
$ git worktree list
/work/wt-lab/demo 9b54f98 [main]
/work/wt-lab/demo-feature 9b54f98 [feature]
この章と次の章のコマンドは、Windows の PowerShell 7 でもそのまま動きました。違うのは、ディレクトリを消す rm -rf(PowerShell では Remove-Item -Recurse -Force)と、一覧を出す ls(本文の確認では Get-ChildItem -Name を使用)だけです。
エラー別の原因と直し方
contains modified or untracked files(変更が残っていて remove できない)
worktree の中のファイルを変更し、新しいファイルも作ってから remove すると、消せませんでした。
$ echo change >> ../demo-feature/README.md
$ echo memo > ../demo-feature/memo.txt
$ git worktree remove ../demo-feature
fatal: '../demo-feature' contains modified or untracked files, use --force to delete it
$ git -C ../demo-feature status --short
M README.md
?? memo.txt
Git の公式ドキュメントでは、remove で消せるのは、追跡していないファイルも、追跡しているファイルの変更もない worktree だけです。変更を残したいときは、その worktree でコミットしてから消します。git -C <パス> を使うと、worktree に cd しなくてもコマンドを実行できます。
$ git -C ../demo-feature add README.md memo.txt
$ git -C ../demo-feature commit -q -m "feature work"
$ echo debug > ../demo-feature/debug.log
$ git -C ../demo-feature status --short
$ git worktree remove ../demo-feature
$ ls ..
demo
コミットのあとに作った debug.log は、.gitignore の *.log で無視されるので、git status に出ません。remove は止まらず、debug.log も worktree ごと消えました。Git のソース(builtin/worktree.c)では、消してよいかを git status --porcelain --ignore-submodules=none の出力で判断していて、この出力に無視しているファイルは含まれません。
変更を捨ててよいときは、--force をつけます。コミットしていない変更は戻せないので、消す前に git status で中身を確かめてください。
$ git worktree add ../demo-try -b try
Preparing worktree (new branch 'try')
HEAD is now at 9b54f98 first commit
$ echo change >> ../demo-try/README.md
$ git worktree remove ../demo-try
fatal: '../demo-try' contains modified or untracked files, use --force to delete it
$ git worktree remove --force ../demo-try
$ git worktree list
/work/wt-lab/demo 9b54f98 [main]
cannot delete branch … used by worktree(ブランチを消せない)
worktree がチェックアウトしているブランチは、git branch -d でも -D でも消せませんでした。
$ git worktree add ../demo-fix -b fix
Preparing worktree (new branch 'fix')
HEAD is now at 9b54f98 first commit
$ git branch -d fix
error: cannot delete branch 'fix' used by worktree at '/work/wt-lab/demo-fix'
$ git branch -D fix
error: cannot delete branch 'fix' used by worktree at '/work/wt-lab/demo-fix'
メッセージに出ているパスの worktree を先に消すと、ブランチを消せました。
$ git worktree remove ../demo-fix
$ git branch -d fix
Deleted branch fix (was 9b54f98).
worktree を消したあとでも、マージしていないコミットがあるブランチは -d では消せません。前の節でコミットした feature ブランチで試しました。
$ git branch -d feature
error: the branch 'feature' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
$ git branch -D feature
Deleted branch feature (was ac41f63).
公式ドキュメントでは、-d で消せるのは、上流のブランチ(設定がなければ HEAD)にすべてマージされたブランチです。-D は --delete --force の短縮形で、マージしていなくてもブランチを消します。消えるのはブランチ(と、そのブランチの reflog)で、コミットそのものがすぐに消えるわけではありません。ただし、ほかのブランチなどから参照されていないコミットは、あとで git gc が不要なものとして消すことがあります(git gc の公式ドキュメント)。コミットが必要なら、消す前に main へマージしてください。
prunable と missing but already registered worktree(ディレクトリを手で消した)
git worktree remove を使わずに、worktree のディレクトリを手で消した場合です。Git は、リポジトリの中(.git/worktrees/)にある worktree の管理情報を消していないので、git worktree list に prunable がついて残りました。
$ git worktree add ../demo-tmp -b tmp
Preparing worktree (new branch 'tmp')
HEAD is now at 9b54f98 first commit
$ rm -rf ../demo-tmp
$ git worktree list
/work/wt-lab/demo 9b54f98 [main]
/work/wt-lab/demo-tmp 9b54f98 [tmp] prunable
$ git worktree list --porcelain
worktree /work/wt-lab/demo
HEAD 9b54f9809c0c1017c379ecc6780c473742180303
branch refs/heads/main
worktree /work/wt-lab/demo-tmp
HEAD 9b54f9809c0c1017c379ecc6780c473742180303
branch refs/heads/tmp
prunable gitdir file points to non-existent location
Windows の PowerShell では、rm -rf ../demo-tmp の代わりに Remove-Item -Recurse -Force ../demo-tmp で消して、同じ結果になりました。
この状態では、ブランチがまだ使われていると見なされて消せません。同じパスに worktree を作り直すこともできませんでした。
$ git branch -d tmp
error: cannot delete branch 'tmp' used by worktree at '/work/wt-lab/demo-tmp'
$ git worktree add ../demo-tmp tmp
Preparing worktree (checking out 'tmp')
fatal: '../demo-tmp' is a missing but already registered worktree;
use 'add -f' to override, or 'prune' or 'remove' to clear
git worktree prune で、ディレクトリがなくなった worktree の管理情報を消します。--dry-run をつけると、消す対象を表示するだけで何も消しません。--verbose をつけると、消したものを表示します。
$ git worktree prune --dry-run --verbose
Removing worktrees/demo-tmp: gitdir file points to non-existent location
$ git worktree prune --verbose
Removing worktrees/demo-tmp: gitdir file points to non-existent location
$ git worktree list
/work/wt-lab/demo 9b54f98 [main]
$ git branch -d tmp
Deleted branch tmp (was 9b54f98).
消したい worktree が1つだけなら、ディレクトリがなくなったあとでも git worktree remove で消せました。
$ git worktree add ../demo-tmp2 -b tmp2
Preparing worktree (new branch 'tmp2')
HEAD is now at 9b54f98 first commit
$ rm -rf ../demo-tmp2
$ git worktree remove ../demo-tmp2
$ git branch -d tmp2
Deleted branch tmp2 (was 9b54f98).
公式ドキュメントによると、このような管理情報は git gc のときにも消されます。git gc は git worktree prune --expire 3.months.ago を呼ぶので、自動で消えるのは3か月より古いものです(設定 gc.worktreePruneExpire で変えられる)。すぐに片付けたいときは、上のように prune を実行します。
cannot remove a locked working tree(ロックされた worktree)
git worktree lock は、外付けのディスクやネットワークの共有フォルダに置いた worktree が、つながっていない間に prune で消されないようにする機能です(公式ドキュメント)。ロックされた worktree は、remove でも remove --force でも消せませんでした。
$ git worktree add ../demo-usb -b usb
Preparing worktree (new branch 'usb')
HEAD is now at 9b54f98 first commit
$ git worktree lock --reason "on an external disk" ../demo-usb
$ git worktree list
/work/wt-lab/demo 9b54f98 [main]
/work/wt-lab/demo-usb 9b54f98 [usb] locked
$ git worktree remove ../demo-usb
fatal: cannot remove a locked working tree, lock reason: on an external disk
use 'remove -f -f' to override or unlock first
$ git worktree remove --force ../demo-usb
fatal: cannot remove a locked working tree, lock reason: on an external disk
use 'remove -f -f' to override or unlock first
メッセージのとおり、unlock してから消します。
$ git worktree unlock ../demo-usb
$ git worktree remove ../demo-usb
$ git branch -d usb
Deleted branch usb (was 9b54f98).
ロックしたままディレクトリを手で消すと、prune は何も消さず、一覧にも locked のまま残りました。この場合も、unlock してから prune すると消えました。
$ git worktree add ../demo-usb2 -b usb2
Preparing worktree (new branch 'usb2')
HEAD is now at 9b54f98 first commit
$ git worktree lock ../demo-usb2
$ rm -rf ../demo-usb2
$ git worktree prune --verbose
$ git worktree list
/work/wt-lab/demo 9b54f98 [main]
/work/wt-lab/demo-usb2 9b54f98 [usb2] locked
$ git worktree unlock ../demo-usb2
$ git worktree prune --verbose
Removing worktrees/demo-usb2: gitdir file points to non-existent location
$ git branch -d usb2
Deleted branch usb2 (was 9b54f98).
ロックを外さずに消すなら、-f(--force)を2回つけます。公式ドキュメントでも、ロックされた worktree を消すには --force を2回指定する、とあります。
$ git worktree add ../demo-usb3 -b usb3
Preparing worktree (new branch 'usb3')
HEAD is now at 9b54f98 first commit
$ git worktree lock ../demo-usb3
$ git worktree remove -f -f ../demo-usb3
$ git branch -d usb3
Deleted branch usb3 (was 9b54f98).
Windows で違ったところ
ここまでの手順は、Windows の Git Bash と PowerShell 7(どちらも Git for Windows 2.50.1)でも同じ結果でした。違ったのは、次の3つです。以下の出力では、長いパスの前半を C:/…/ と省略しています。
パスの表示と、改行コードの警告
git worktree list やエラーのメッセージのパスは、C:/…/wt-lab/demo-fix のように、ドライブ名とスラッシュで表示されました。Git Bash では、git add のときに warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it という警告も出ました。このPCの Git for Windows のシステムの設定(core.autocrlf=true)によるもので、worktree の後始末とは関係ありません。PowerShell 7 の echo で作ったファイルでは、この警告は出ませんでした。
ほかのプロセスが使っていると、フォルダが残る
別の端末をその worktree で開いたままにした状態の代わりに、作業ディレクトリを worktree にした別の PowerShell を起動しておき、remove しました。PowerShell 7 での結果です。
PS> git worktree add ../demo-busy -b busy
Preparing worktree (new branch 'busy')
HEAD is now at 0782d57 first commit
PS> $p = Start-Process pwsh -WorkingDirectory ../demo-busy -ArgumentList '-NoProfile','-Command','Start-Sleep 30' -WindowStyle Hidden -PassThru
PS> git worktree remove ../demo-busy
error: failed to delete 'C:/…/wt-lab/demo-busy': Permission denied
PS> git worktree list
C:/…/wt-lab/demo 0782d57 [main]
PS> Get-ChildItem .. -Name
demo
demo-busy
PS> Get-ChildItem ../demo-busy -Force -Name
remove は失敗しましたが、git worktree list からは消えていて、中身が空のフォルダ demo-busy が残りました。Git のソース(builtin/worktree.c)でも、作業ツリーの削除に失敗しても、管理情報の削除は続けて行う作りになっています。
使っているプロセスを終わらせてから、もう一度 remove しても、Git の管理から外れているので「not a working tree」になりました。残ったフォルダを手で消し、ブランチを消しました。
PS> Stop-Process -Id $p.Id
PS> git worktree remove ../demo-busy
fatal: '../demo-busy' is not a working tree
PS> Remove-Item -Recurse -Force ../demo-busy
PS> git branch -d busy
Deleted branch busy (was 0782d57).
Linux のコンテナで、別のプロセスの作業ディレクトリを worktree の中にした場合と、別のプロセスが worktree の中のファイルを開いている場合を試すと、どちらも remove は成功し、ディレクトリも残りませんでした。
Windows では、次の場合も同じように失敗して、フォルダが残りました。
- worktree の中に
cdして、git worktree remove .を実行した(Git Bash と PowerShell 7 の両方でPermission denied。中身が空のフォルダが残った)。Linux では、同じコマンドで成功した。 - 別のプログラムが、worktree の中のファイルを、削除を許さない設定で開いている(PowerShell 7 の
[IO.File]::Openで再現。error: failed to delete '...': Invalid argumentになり、開いていたREADME.mdが残った)
どちらも、直し方は同じです。worktree の外に cd するか、ファイルを開いているプログラムを閉じてから、残ったフォルダを Remove-Item -Recurse -Force(Git Bash なら rm -rf)で消します。
対話の端末では「Should I try again? (y/n)」と聞かれる
Git for Windows のソース(compat/mingw.c)では、ファイルやフォルダの削除に失敗すると、少し待って何回か試し直し、それでも失敗したら「もう一度試すか」を聞きます。聞くのは、入力とエラー出力の両方が端末のときだけで、それ以外は「いいえ」として扱われます。今回の確認はスクリプトから実行したので、質問は出ずに上のエラーになりました。
質問の文は、Git for Windows の環境変数 GIT_ASK_YESNO に、質問を表示して「いいえ」と答える小さなスクリプトを指定して確かめました(先頭の (GIT_ASK_YESNO) は、そのスクリプトがつけたものです)。
PS> git worktree remove ../demo-open2
(GIT_ASK_YESNO) Unlink of file 'C:/…/wt-lab/demo-open2/README.md' failed. Should I try again?
error: failed to delete 'C:/…/wt-lab/demo-open2': Invalid argument
PS> git worktree remove .
(GIT_ASK_YESNO) Deletion of directory 'C:/…/wt-lab/demo-here2' failed. Should I try again?
error: failed to delete 'C:/…/wt-lab/demo-here2': Permission denied
1つ目はファイルを開いたまま、2つ目は demo-here2 の中に cd して実行したものです。ソースでは、y と答えると削除を試し直し、n と答えると上のエラーになります。端末でこの質問が出たときは、原因で答え方が変わります。
- 別のプロセス(別の端末、ファイルを開いているプログラム)が原因なら、そのプロセスを終わらせてから
yと答えれば、試し直しで消せる見込みです。 - Git を実行したシェル自身が worktree の中にいる(
git worktree remove .など)なら、答えている間もその状態は変わらないので、yでは消せません。nで終わらせ、worktree の外にcdしてから、残ったフォルダを手で消します。
どちらも、人が端末で操作して答える確認はしていません。

コメント