24

I work on a team on a large repo. Recently we decided to move one of the folders into its own submodule

-- aaa
     -- .git
     --  bbb
     --  ccc
     --  www      # this folder is going into its own repo.

I followed the instructions to filter out the www folder into its own repo listed here: Detach (move) subdirectory into separate Git repository. I moved the www folder out of the aaa repo.

I removed the directory from the master branch by running these commands:

 $ cd aaa
 $ git checkout master
 $ git rm -rf www
 $ git commit -m "remove the www/ folder from the aaa repo."

So now on master, the tree looks like this:

 -- aaa
     -- .git
     --  bbb
     --  ccc

I'd like to add www as a submodule by running:

$ cd aaa
$ git checkout master
$ git submodule add git@bitbucket.org:kevinburke/www.git www
Cloning into 'www'...
remote: Counting objects: 717, done.
remote: Compressing objects: 100% (392/392), done.
remote: Total 717 (delta 318), reused 711 (delta 317)
Receiving objects: 100% (717/717), 440.52 KiB | 58 KiB/s, done.
Resolving deltas: 100% (318/318), done.

That works fine on master. However, any time I try to switch to another branch, I get the following error:

$ cd aaa
$ git checkout other-old-branch
error: The following untracked working tree files would be overwritten by checkout:
    www/1...
    www/2...
    www/3...
    www/4...
Aborting

How can I remove the www folder from all the branches in the aaa repo? There are about 100 branches, so doing this manually would be a hassle.

I'm not worried about keeping any outstanding changes that exist in www folders of older branches.

Community
  • 1
  • 1
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305

2 Answers2

35

Just use git checkout -f to swap branches, then remove them like you normally would and merge in master to get your submodule introduction.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Note that if you do a back-and-forth between branches, and the paths have overlapping file names, these files get deleted instead of being reset to the original content. – slhck Jul 20 '21 at 12:28
  • Can also happen in the reverse situation where the submodule was introduced on a branch, but not merged to master yet. In which case you just need to `git checkout -f`, without removing anything if you're moving to such a branch. – Fabien Haddadi Dec 09 '21 at 20:52
  • Better yet, use `git checkout -f --recurse-submodules` – XKpe Nov 01 '22 at 15:01
-1

Since you have deleted the path only on working branch. on all other branches the path is available. So your submodule will cause conflict.

nibinbhaskar
  • 177
  • 1
  • 4