Track file in master repository which is ignored in submodule

135 views Asked by At

I have a structure like this:

/.gitignore
/submodule/.gitignore
/submodule/file.txt

Inside my submodule the .gitignore file ignores file.txt. That is very much logical for the submodule. But in my root I would like to add "file.txt" to the repo. Is that even possible?

!submodule/file.txt

Adding the line above to /.gitignore does not work. So for my main repo I would like to overwrite and add it to git but the submodule prevents me from versioning the file.

2

There are 2 answers

0
Anshul Goyal On BEST ANSWER

Nopes, you can't do that. Any file within the submodule can not be added to the master project. It doesn't matter whether it is tracked or not, it just can't be added since the namespace overlaps.

If you really need to do this, then have this file tracked in some other directory (not a submodule) within the master repository, and create a symlink to it in your submodule.

So your structure becomes something like:

/.gitignore
/submodule/.gitignore
/submodule/symlink_to_others_file.txt
/others/file.txt
0
Alexey Andrushkevich On

Actually .gitignore from submodule doesn't affect root repository and vice versa. You have to treat root repository and submodule repository as 2 different repositories with their own .gitignore files. So if you added file.txt to rootrepo/submodule/.gitignore you will not be able to place file.txt under submodule folder unless you force git to do this (git add --force file.txt). At the same time you have to be absolutely OK with adding rootrepo/file.txt.