简体   繁体   English

添加到 Git 子模块中的 .gitignore 文件

[英].gitignore files added inside Git submodules

I recently reorganized my dotfiles to live inside a Git repository at ~/Dropbox/dotfiles and I'm using pathogen to bundle all Vim addons inside ~/Dropbox/dotfiles/home/.vim/bundle .我最近重新组织了我的 dotfiles 以位于~/Dropbox/dotfiles的 Git 存储库中,并且我正在使用 pathogen 将所有 Vim 插件捆绑在~/Dropbox/dotfiles/home/.vim/bundle These addons were added as Git submodules.这些插件是作为 Git 子模块添加的。

Now the problem is, when I run Vim, it automatically generates the documentation for all addons and puts them inside each submodule directory.现在的问题是,当我运行 Vim 时,它会自动为所有插件生成文档并将它们放在每个子模块目录中。 This adds untracked content to the submodules, which I'd like to avoid.这会向子模块添加未跟踪的内容,我想避免这种情况。

ruby-1.8.7-p330@gs ~/Dropbox/dotfiles ‹master*› $ git st
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)
#
#   modified:   home/.vim/bundle/fuzzyfinder (untracked content)
#   modified:   home/.vim/bundle/l9 (untracked content)
#   modified:   home/.vim/bundle/matchit (untracked content)
#   modified:   home/.vim/bundle/ruby (untracked content)
#   ...
no changes added to commit (use "git add" and/or "git commit -a")

I tried to add a .gitignore file to the root of my Git repository to ignore all doc folders inside submodules, but this doesn't seem to work:我尝试将.gitignore文件添加到我的 Git 存储库的根目录以忽略子模块内的所有doc文件夹,但这似乎不起作用:

home/.vim/bundle/**/doc

My question: is there a way to ignore files and folders inside Git submodules or maybe configure Vim to create the documentation in a folder outside the Git repository?我的问题:有没有办法忽略 Git 子模块内的文件和文件夹,或者配置 Vim 以在 Git 存储库外的文件夹中创建文档?

EDIT: as Randy Morris pointed out, this might be a duplicate of Generating tags to different location by pathogen编辑:正如 Randy Morris 指出的,这可能是Generating tags to different location by pathogen的重复

You should add .gitignore within each of your submodules.您应该在每个子模块中添加.gitignore
Since said submodules are like nested Git repo, they take care of their own ignore rules, and their status wouldn't be influenced by the .gitignore of the parent repo (as explained here ).由于所述子模块就像嵌套的 Git 存储库,它们会处理自己的忽略规则,并且它们的状态不会受到父存储库的.gitignore的影响(如此处解释)。

For a vim-specific setting, as Randy Morris mentions in the comment, see the SO question " Generating tags to different location by pathogen ".对于特定于 vim 的设置,正如Randy Morris在评论中提到的,请参阅 SO 问题“ 通过病原体将标签生成到不同位置”。


Note: as Nick mentions in this comments, and as illustrated by the answer to " Generating tags to different location by pathogen ", a config like:注意:正如Nick在此评论中提到的,以及如“ 通过病原体将标签生成到不同位置”的答案所示,配置如下:

[submodule "path/to/submodule"]
    path = path/to/submodule
    url = http://github.com/username/repo.git
    ignore = untracked  # <====

will work and make that submodule ignored by git status .将工作并使该子模块被git status忽略。
But " ignore = untracked " means at least Git1.7.2 .但是“ ignore = untracked untracked至少意味着Git1.7.2

The config is usually found in the .git/ folder of your repo.该配置通常位于您的 repo 的.git/文件夹中。


Note: nurettin mentions in the comments :注意: nurettin 在评论中提到:

ignore = dirty did it for me ignore = dirty为我做的

Commit aee9c7d details the difference between untracked and dirty . 提交aee9c7d细节之间的差异untrackeddirty

  • " dirty ": Only differences of the commit recorded in the superproject and the submodules HEAD will be considered modifications, all changes to the work tree of the submodule will be ignored . dirty ”:只有超级项目和子模块HEAD记录的提交的差异才会被认为是修改,对子模块的工作树的所有更改都将被忽略
    When using this value, the submodule will not be scanned for work tree changes at all, leading to a performance benefit on large submodules .使用此值时,根本不会扫描子模块的工作树更改,从而提高大型子模块的性能

  • " untracked ": Only untracked files in the submodules work tree are ignored, a changed HEAD and/or modified files in the submodule will mark it as modified. untracked ”:仅忽略子模块工作树中未跟踪的文件,子模块中已更改的HEAD和/或修改的文件会将其标记为已修改。

VonC's recommendation of ignore = untracked works, but you have to make the change for every such submodule. VonC 的ignore = untracked建议是有效的,但是您必须对每个这样的子模块进行更改。

If you want to solve the problem once and for all, you can configure a global gitignore pattern on every git repo, by git config --global core.excludesfile ~/.gitignore_global , then just add doc/tags to $HOME/.gitignore_global.如果你想一劳永逸地解决问题,你可以在每个 git repo 上配置一个全局 gitignore 模式,通过git config --global core.excludesfile ~/.gitignore_global ,然后只需将doc/tags添加到 $HOME/.gitignore_global .

See https://help.github.com/articles/ignoring-files .请参阅https://help.github.com/articles/ignoring-files

Ben's answer is a good start. Ben 的回答是一个好的开始。 But for such specific ignore rule, I don't like the global setting.但是对于这种特定的忽略规则,我不喜欢全局设置。

Prefer local config for each submodule, and a centralized ignore file in submodule root repository:首选每个子模块的本地配置,以及子模块根存储库中的集中忽略文件:

cd .vim/bundle
echo "doc/tags" > .gitignore_submodules
git submodule foreach git config --local core.excludesfile "../.gitignore_submodules"

I think the basic problem here is that you are trying to ignore a 'tracked' change.我认为这里的基本问题是您试图忽略“跟踪”更改。 .gitignore ignores only 'untracked' changes to the repo. .gitignore仅忽略对 repo 的“未跟踪”更改。 To verify you can modify some file in your repo (which tracked) and also add it to your .gitignore file.要验证您可以修改您的存储库中的某些文件(已跟踪)并将其添加到您的.gitignore文件中。 On doing git status you will see that the file is still listed as 'modified'.在执行git status您会看到该文件仍列为“已修改”。 Your submodule repo's are also considered tracked by your main repo, hence simply adding a gitignore to the parent repo won't work.您的子模块存储库也被认为是由主存储库跟踪的,因此简单地向父存储库添加 gitignore 是行不通的。 As othere suggested, either add .gitignore rules individual repo's or if you're using a recent git version, you can use the git status --ignore-submodules .正如其他人所建议的,要么添加.gitignore规则单独的回购,或者如果您使用的是最近的 git 版本,您可以使用git status --ignore-submodules

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

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