您是忽略 .gitignore 中的 git 子模块还是将其提交到您的存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7912022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:10:35  来源:igfitidea点击:

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

git

提问by sprysoft

I've added a submodule to my project in project_dir/vendor/submodule_onenow each time I run git statusI get modified: vendor/submodule_one (new commits).

project_dir/vendor/submodule_one现在每次运行时都向我的项目添加了一个子模块,我git status得到modified: vendor/submodule_one (new commits).

My question is whats the best way to deal with this? Do I add the vendor/submodule_one-folder to my .gitignoreas my main project shouldn't need to know about the specifics of my submodule?

我的问题是处理这个问题的最佳方法是什么?我是否将vendor/submodule_one-folder添加到我的.gitignore主项目中,因为我的主项目不需要了解我的子模块的细节?

Or when I'm changing and commiting changes to my submodule do I need to make commits in my main project also?

或者当我更改和提交对我的子模块的更改时,我是否还需要在我的主项目中进行提交?

Just getting started with submodules and couldn't seem to find much info beyond setting them up.

刚刚开始使用子模块,除了设置它们似乎找不到太多信息。

采纳答案by VonC

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).

不,您不需要将您的子模块添加到您的.gitignore: 父模块将从您的子模块中看到的是gitlink(一个特殊条目,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;

这意味着:在子模块中直接进行的任何更改都需要在父目录中进行提交。
这样,父目录就会为子模块的状态记录正确的提交:那个提交就是上面提到的“gitlink”;

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.

您可以在“ git submodule update (true nature of submodules)”中阅读有关该政策的更多信息。
子模块背后的主要思想是基于组件的方法,您可以在其中引用特定提交时的其他存储库。但是,如果您更改了这些子模块中的任何内容,则还需要更新父存储库中的这些引用。



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

请注意,在 Git 2.13(2017 年第二季度)中,虽然忽略 gitlink,但您仍然可以忽略子模块:

git config submodule.<name>.active false

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

在“忽略 git 子模块的新提交”中查看更多信息。



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.

注意:对于 Git 2.15.x/2.16(2018 年第一季度),忽略子模块更准确。
" git status --ignored --untracked" 并没有停留在一个单独项目的工作树上,该项目嵌入在一个被忽略的目录中,并在该其他项目中列出了文件,而不仅仅是将目录本身显示为被忽略。

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

请参阅Johannes Schindelin ( )提交的 fadb482(2017 年 10 月 25 日)(由Junio C Hamano合并-- --da7996a 提交中,2017 年 11 月 6 日)dscho
gitster

status: do not get confused by submodules in excluded directories

We meticulously pass the excludeflag 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 --untrackedwith a submodule submodulein a gitignored tracked/would show the submodule in the "Untracked files" section, e.g.

status: 不要被排除目录中的子模块混淆

我们精心地将exclude标志传递给treat_directory()函数,以便我们可以指示在递归时排除其中的文件而不是未跟踪的文件。

但是我们还没有以同样的方式对待子模块。

正因为如此,git status --ignored --untracked有一个子模块 submodule在gitignoredtracked/将显示辅助模块中的“中Untracked files”部分,例如:

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:

相反,我们希望它在“ Ignored files”部分显示子模块:

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

    tracked/submodule/

回答by Vladimir

For some reason submodule.module-name.activedidn't work for me.

出于某种原因submodule.module-name.active对我不起作用。

That's why I used submodule.module-name.ignore

这就是为什么我使用submodule.module-name.ignore

git config submodule.<your module path>.ignore all

https://git-scm.com/docs/gitmodules- here you can find description of possible values for parameter

https://git-scm.com/docs/gitmodules- 在这里您可以找到参数可能值的描述

Works for me for (new commits) and (modified content) messages.

适用于(新提交)和(修改内容)消息。