简体   繁体   English

Git:忽略子模块中未跟踪的文件

[英]Git: Ignore untracked files in submodule

In my git repo, I have imported some other project as a submodule. 在我的git repo中,我已经导入了一些其他项目作为子模块。 So far, so good. 到现在为止还挺好。 However, the maintainers of the imported project are a bit sloppy about their .gitignore files. 但是,导入项目的维护者对他们的.gitignore文件有点草率。 So, after building the imported project, git status (within the submodule) lists a ton of untracked files. 因此,在构建导入的项目之后, git status (在子模块中)列出了大量未跟踪的文件。 And consequently, git status in my own project says: 因此,我自己的项目中的git status说:

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? 我的问题是:有没有办法告诉git忽略这些未跟踪的文件而不修复上游的.gitignore文件?


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. 但是,我只关心忽略未跟踪的文件,所以我认为我的问题有一个很好的解决方案。

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. 最灵活的方法是在子模块的gitdir / info / exclude文件中添加忽略规则。

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. 添加模式,因为它将出现在子模块repo的.gitignore文件中。

You can confirm where the submodule's gitdir is by 您可以确认子模块的gitdir所在的位置

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. 但这种方法的gitdirgitdir / info / exlcude不会通过推送或提取传播,因此必须单独配置每个repo。

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. 如果您不需要对被忽略的文件如此具有选择性 - 例如,如果您可以说“应忽略子模块中的任何工作树更改” - 那么您可以在模块定义中指定忽略规则(.gitmodules ),这当然是承诺工作的一部分。 Possible ignore rules are explained in the gitmodules documentation: https://git-scm.com/docs/gitmodules gitmodules文档中解释了可能的忽略规则: https ://git-scm.com/docs/gitmodules

In .gitmodules add ignore = all to submodule. .gitmodulesignore = all添加到子模块。
There more options for ignore , you can read them in git help config , search submodule.<name>.ignore . 有更多的ignore选项,你可以在git help config ,search submodule.<name>.ignore读取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM