0

Let's say I have the following structure:

my-project
├── modules
│   ├── my-first-module
│   ├── my-second-module
│   └── my-fourth-module

I included the modules directory in my .gitignore file and made an exception for my-third-module:

/my-project/modules/*
!/my-project/modules/my-third-module/

Then when I execute git submodule add git@git.my-app/my-third-module.git from my-project/modules/ path, I get:

The following path is ignored by one of your .gitignore files:
my-project/modules/my-third-module
Use -f if you really want to add it.

It seems the exclusion of /my-project/modules/my-third-module/ in .gitignore is not taken into account.

So, I tried to create an empty my-third-module directory but in this case the git submodule add command complains:

'my-third-module' already exists and is not a valid git repo

Apparently git submodule add requires that the directory doesn't exists, so it's not the way to go.

Why the exclusion of my-third-module is not taken into account by Git?

Kwadz
  • 2,206
  • 2
  • 24
  • 45

1 Answers1

0

As mentioned in this answer:

Unless --force is specified, submodule add checks if the destination path is ignored by calling git add --dry-run --ignore-missing

So, I executed git add --dry-run --ignore-missing my-third-module and I got:

The following paths are ignored by one of your .gitignore files:
my-project/modules/my-third-module
Use -f if you really want to add them.

That made me suspect that there was probably an issue with excluding in .gitignore a directory that doesn't exist.

After I created manually my-third-module, I executed again the git add command and now it's OK.
That means Git needs the directory to be created to be able to exclude it. But since it's created it's well excluded.
The command git submodule add simply doesn't take charge of this automatically.

Therefore it's safe using -f in this case to add the submodule:

git submodule add -f git@git.my-app/my-third-module.git

The my-third-module directory will be well excluded from being ignored by Git after its creation.

Kwadz
  • 2,206
  • 2
  • 24
  • 45