Home > Blockchain >  How do I remove a Git worktree that contains a submodule?
How do I remove a Git worktree that contains a submodule?

Time:11-28

Let’s say that I have a Git repo that contains a submodule:

$ mkdir main-worktree
$ cd main-worktree
$ git init
Initialized empty Git repository in /…/main-worktree/.git/
$ git submodule add https://github.com/octocat/Hello-World.git Hello-World
Cloning into '/…/main-worktree/Hello-World'...
remote: Enumerating objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Receiving objects: 100% (13/13), done.
$ git commit -m 'Create submodule'
[main (root-commit) 66070b6] Create submodule
 2 files changed, 4 insertions( )
 create mode 100644 .gitmodules
 create mode 160000 Hello-World

and then create another worktree for that repo (one that contains the submodule):

$ git worktree add ../secondary-worktree
Preparing worktree (new branch 'secondary-worktree')
HEAD is now at 66070b6 Create submodule
$ cd ../secondary-worktree
$ git submodule update
Cloning into '/…/secondary-worktree/Hello-World'...
Submodule path 'Hello-World': checked out '7fd1a60b01f91b314f59955a4e4d4e80d8edf11d'

How do I remove the secondary-worktree that I just created? I’ve tried using git worktree remove, but it gave me an error:

$ cd <path-to-main-worktree>
$ git worktree remove ../secondary-worktree
fatal: working trees containing submodules cannot be moved or removed

CodePudding user response:

You can use --force to remove a worktree that contains submodules:

git worktree remove --force <path-to-worktree-with-submodules>
  • Related