Do you ignore a git submodule in your .gitignore or commit it to your repo?

No, you don’t need to add your submodule to your .gitignore: what the parent will see from your submodule is a gitlink (a special entry, mode 160000).

That means: any change directly made in a submodule needs to be followed by a commit in the parent directory.
That way, the parent directory will record the right commit for the state of the submodule: That commit is the “gitlink” mentioned above;

You can read more about that policy in “git submodule update (true nature of submodules)“.
The main idea behind submodules is a component-based approach, where you reference other repos at specific commits. But if you change anything in those submodules, you need to update those references in the parent repo as well.


Note that with Git 2.13 (Q2 2017), while not ignoring the gitlink, you can still ignore the submodule with:

git config submodule.<name>.active false

See more at “Ignore new commits for git submodule“.


Note: with Git 2.15.x/2.16 (Q1 2018), ignoring a submodule is more precise.
git status --ignored --untracked” did not stop at a working tree of a separate project that is embedded in an ignored directory and listed files in that other project, instead of just showing the directory itself as ignored.

See commit fadb482 (25 Oct 2017) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano — gitster in commit da7996a, 06 Nov 2017)

status: do not get confused by submodules in excluded directories

We meticulously pass the exclude flag to the treat_directory() function so that we can indicate that files in it are excluded rather than untracked when recursing.

But we did not yet treat submodules the same way.

Because of that, git status --ignored --untracked with a submodule
submodule in a gitignored tracked/ would show the submodule in the
Untracked files” section, e.g.

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    tracked/submodule/

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

    tracked/submodule/initial.t

Instead, we would want it to show the submodule in the “Ignored files
section:

On branch master
Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

    tracked/submodule/

Leave a Comment