Search code examples
gitgit-submodulesgitignore

Git: Ignore untracked files in submodule


In my git repo, I have imported some other project as a submodule. So far, so good. However, the maintainers of the imported project are a bit sloppy about their .gitignore files. So, after building the imported project, git status (within the submodule) lists a ton of untracked files. And consequently, git status in my own project says:

modified:   <submodule> (untracked content)

My question is: Is there any way of telling git to ignore these untracked files without fixing the upstream .gitignore files?


I'm fully aware that the likely answer is "no", and I have seen this SO question that tells me that it is not possible to ignore changes to tracked files. Which makes total sense to me, independent of the question whether the file is in a submodule or not. However, I'm only concerned with ignoring untracked files, so I think there is a slight chance that there is a good solution for my question.


Solution

  • UPDATE - In comments the question came up: can the parent repo automatically distribute these ignore rules. If you use the original approach I suggest, the answer is "no" to the best of my knowledge. However, under some circumstances there is a way to do it (from Роман Яковский's answer)...


    The most flexible way would be to add ignore rules in the submodule's gitdir/info/exclude file.

    cd path/to/parent/repo/.git/modules/module-name/info
    vi exclude
    

    add the pattern as it would appear in the submodule repo's .gitignore file.

    You can confirm where the submodule's gitdir is by

    cat path/to/parent/repo/submoduledir/.git
    

    This lets you specify exactly what should be ignored; but the down side to this approach is that gitdir/info/exlcude is not propagated by push or fetch, so each repo must be individually configured.

    If you don't need to be so selective about the files that are ignored - for example, if you can say "any worktree changes in the submodule should be ignored" - then you can specify an ignore rule in the module definition (.gitmodules), which of course is part of the committed work. Possible ignore rules are explained in the gitmodules documentation: https://git-scm.com/docs/gitmodules