忽略 git 子模块的新提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14418352/
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 15:18:45  来源:igfitidea点击:

Ignore new commits for git submodule

gitstatusgit-submodulesignore

提问by Dave Jarvis

Background

背景

Using Git 1.8.1.1 on Linux. The repository looks as follows:

在 Linux 上使用 Git 1.8.1.1。存储库如下所示:

master
  book

The submodule was created as follows:

子模块创建如下:

$ cd /path/to/master
$ git submodule add https://[email protected]/user/repo.git book

The booksubmodule is clean:

book子模块是干净的:

$ cd /path/to/master/book/
$ git status
# On branch master
nothing to commit, working directory clean

Problem

问题

The master, on the other hand, shows there are "new commits" for the book submodule:

另一方面,master 显示 book 子模块有“新提交”:

$ cd /path/to/master/
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   book (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")

Git should ignore the submodule directory completely, so that the master is also clean:

Git 应该完全忽略子模块目录,这样 master 也是干净的:

$ cd /path/to/master/
$ git status
# On branch master
nothing to commit, working directory clean

Failed Attempt #1 - dirty

失败的尝试 #1 - 脏

Inside the file master/.gitmodulesis the following, as per this answer:

master/.gitmodules根据这个答案,文件内部如下:

[submodule "book"]
        path = book
        url = https://[email protected]/user/repo.git
        ignore = dirty

Failed Attempt #2 - untracked

失败的尝试 #2 - 未跟踪

Changed master/.gitmodulesto the following, as per this answer:

master/.gitmodules根据此答案更改为以下内容:

[submodule "book"]
        path = book
        url = https://[email protected]/user/repo.git
        ignore = untracked

Failed Attempt #3 - showUntrackedFiles

失败的尝试 #3 - showUntrackedFiles

Edited master/.git/configto the following, as per this answer:

master/.git/config根据此答案编辑为以下内容:

[status]
   showUntrackedFiles = no

Failed Attempt #4 - ignore

失败的尝试 #4 - 忽略

Added the book directory to the master ignore file:

将 book 目录添加到 master 忽略文件中:

$ cd /path/to/master/
$ echo book > .gitignore

Failed Attempt #5 - clone

失败的尝试 #5 - 克隆

Added the book directory to the master as follows:

将 book 目录添加到 master 中,如下所示:

$ cd /path/to/master/
$ rm -rf book
$ git clone https://[email protected]/user/repo.git book

Question

How can the booksubmodule be in its own repository directory under the masterrepository yet have git ignore the booksubmodule? That is, the following should not display:

book子模块如何在存储库下的自己的存储库目录中master却让 git 忽略book子模块?也就是说,不应显示以下内容:

#
#       modified:   book (new commits)
#

How to suppress that message when executing git statusin the master repository?

git status在主存储库中执行时如何抑制该消息?

An article about git submodule pitfallssuggests this an inappropriate submodule usage?

一篇关于git submodule pitfalls的文章表明这是一个不适当的子模块使用?

采纳答案by Nevik Rehnel

To include another repository, that needn't be tracked in its super-repo, try this:

要包含另一个不需要在其超级存储库中跟踪的存储库,请尝试以下操作:

$ cd /path/to/master/
$ rm -rf book
$ git clone https://[email protected]/user/repo.git book
$ git add book
$ echo "book" >> .gitignore

Then commit.

然后提交。

As stated in the linked git submodule pitfalls article:

如链接的git 子模块陷阱文章中所述

... the only linkage between the parent and the submodule is [the] recorded value of the submodule's checked-out SHA which is stored in the parent's commits.

...父模块和子模块之间的唯一链接是存储在父模块提交中的子模块检出 SHA 的 [the] 记录值。

That means that a submodule is not saved by its checked-out branch or tag, but always by a specific commit; that commit (SHA) is saved into the super-repo (the one containing the submodule) like a normal text file (it's marked as such a reference, of course).

这意味着子模块不是由其检出的分支或标签保存,而是由特定的提交保存;该提交(SHA)像普通文本文件一样保存到超级存储库(包含子模块的存储库)中(当然,它被标记为这样的引用)。

When you check out a different commit in the submoduleor make a new commit in it, the super-repo will see that its checked out SHA has changed. That's when you get the modified (new commits)line from git status.

当您在子模块中检出不同的提交或在其中进行新的提交时,超级存储库将看到其检出的 SHA 已更改。那是你modified (new commits)git status.

To eliminate that, you can either:

要消除这种情况,您可以:

  • git submodule update, which will reset the submodule to the commit currently saved in the super-repo (for details see the git submodulemanpage; or
  • git add book && git committo save the new SHA into the super-repo.
  • git submodule update,这将重置为提交当前保存在超级回购(详见子模块git submodule手册页;或
  • git add book && git commit将新的 SHA 保存到超级存储库中。

As mentioned in the comments, consider abandoning the booksubmodule: clone it inside the super-repo, if tracking of its state as part of the super-repo is not necessary.

如评论中所述,考虑放弃book子模块:如果不需要将其状态作为超级存储库的一部分进行跟踪,则将其克隆到超级存储库中。

回答by Shiv Kumar

Just run:

赶紧跑:

$ git submodule update

This will revert the submodule the to old commit (specified in parent-repo), without updating the parent-repo with the latest version of the submodule.

这会将子模块还原为旧提交(在 parent-repo 中指定),而不用最新版本的子模块更新 parent-repo。

回答by greuze

There are two kinds of change notices you can suppress (from git 1.7.2).

您可以抑制两种更改通知(从 gi​​t 1.7.2 开始)。

The first is untracked content which happens when you make changes to your submodule but have not yet committed those. The parent repository notices these and git status reports it accordingly:

第一个是未跟踪的内容,它发生在您对子模块进行更改但尚未提交时。父存储库注意到这些并且 git status 相应地报告它:

modified: book (untracked content)

You can suppress these with :

您可以使用以下方法抑制这些:

[submodule "book"]
    path = modules/media
    url = https://[email protected]/user/repo.git
    ignore = dirty

However, once you commit those changes, the parent repository will once again take notice and report them accordingly:

但是,一旦您提交这些更改,父存储库将再次注意到并相应地报告它们:

modified:   book (new commits)

If you want to suppress these too, you need to ignore all changes

如果您也想抑制这些,则需要忽略所有更改

[submodule "book"]
    path = book
    url = https://[email protected]/user/repo.git
    ignore = all

回答by VonC

Git 2.13 (Q2 2017) will add another way to include a submodule which does not need to be tracked by its parent repo.

Git 2.13(2017 年第二季度)将添加另一种方法来包含不需要由其父存储库跟踪的子模块。

In the OP's case:

在 OP 的情况下:

git config submodule.<name>.active false

See commit 1b614c0, commit 1f8d711, commit bb62e0a, commit 3e7eaed, commit a086f92(17 Mar 2017), and commit ee92ab9, commit 25b31f1, commit e7849a9, commit 6dc9f01, commit 5c2bd8b(16 Mar 2017) by Brandon Williams (mbrandonw).
(Merged by Junio C Hamano -- gitster--in commit a93dcb0, 30 Mar 2017)

提交1b614c0提交1f8d711提交bb62e0a提交3e7eaed提交a086f92(2017年3月17日),以及提交ee92ab9提交25b31f1提交e7849a9提交6dc9f01提交5c2bd8b(2017年3月16日)由布兰登威廉姆斯(mbrandonw
(由Junio C gitsterHamano合并-- --in commit a93dcb0,2017 年 3 月 30 日)

submodule: decouple url and submodule interest

Currently the submodule.<name>.urlconfig option is used to determine if a given submodule is of interest to the user. This ends up being cumbersome in a world where we want to have different submodules checked out in different worktrees or a more generalized mechanism to select which submodules are of interest.

In a future with worktree support for submodules, there will be multiple working trees, each of which may only need a subset of the submodules checked out.
The URL (which is where the submodule repository can be obtained) should not differ between different working trees.

It may also be convenient for users to more easily specify groups of submodules they are interested in as opposed to running "git submodule init <path>" on each submodule they want checked out in their working tree.

To this end two config options are introduced, submodule.activeand submodule.<name>.active.

  • The submodule.activeconfig holds a pathspec that specifies which submodules should exist in the working tree.
    • The submodule.<name>.activeconfig is a boolean flag used to indicate if that particular submodule should exist in the working tree.

Its important to note that submodule.activefunctions differently than the other configuration options since it takes a pathspec.
This allows users to adopt at least two new workflows:

  1. Submodules can be grouped with a leading directory, such that a pathspec e.g. 'lib/' would cover all library-ish modules to allow those who are interested in library-ish modules to set "submodule.active = lib/" just once to say any and all modules in 'lib/' are interesting.
  2. Once the pathspec-attribute feature is invented, users can label submodules with attributes to group them, so that a broad pathspec with attribute requirements, e.g. ':(attr:lib)', can be used to say any and all modules with the 'lib' attribute are interesting.
    Since the .gitattributesfile, just like the .gitmodulesfile, is tracked by the superproject, when a submodule moves in the superproject tree, the project can adjust which path gets the attribute in .gitattributes, just like it can adjust which path has the submodule in .gitmodules.

submodule: 解耦 url 和子模块兴趣

目前submodule.<name>.urlconfig 选项用于确定用户是否对给定的子模块感兴趣。在我们想要在不同的工作树中检出不同的子模块或更通用的机制来选择感兴趣的子模块的世界中,这最终会变得很麻烦。

将来工作树支持子模块时,将会有多个工作树,每个工作树可能只需要检出子模块的一个子集。
URL(可以获取子模块存储库的位置)在不同的工作树之间不应不同。

用户可以更方便地指定他们感兴趣的子模块组,而不是git submodule init <path>在他们想要在工作树中检出的每个子模块上运行“ ”。

为此,引入了两个配置选项,submodule.activesubmodule.<name>.active.

  • submodule.active配置包含一个路径规范,用于指定工作树中应存在哪些子模块。
    • submodule.<name>.active配置是用来表示该特定子模块,应在工作树存在的布尔标志。

重要的是要注意它的submodule.active功能与其他配置选项不同,因为它采用路径规范。
这允许用户采用至少两个新的工作流程:

  1. 子模块可以用一个前导目录分组,这样一个路径规范,例如“ lib/”将覆盖所有库模块,以允许那些对库模块感兴趣的人设置“ submodule.active = lib/”一次,以说明“ ”中的任何和所有模块lib/是有趣的。
  2. 一旦发明了 pathspec-attribute 特性,用户就可以用属性标记子模块以将它们分组,这样具有属性要求的广泛路径规范(例如“ :(attr:lib)”)可用于表示具有“ lib”属性的任何和所有模块都很有趣。
    由于.gitattributes文件和.gitmodules文件一样,由超级项目跟踪,当子模块在超级项目树中移动时,项目可以调整哪个路径获取属性.gitattributes,就像它可以调整子模块在哪个路径.gitmodules

回答by Alexis Wilke

Nevik Rehnel answer is certainly the correct one for what you are asking: I did not want to have a submodule, how the heck do I get out of that situation?!.

Nevik Rehnel 的回答肯定是您所问的正确答案:我不想拥有子模块,我该如何摆脱这种情况?!.

Only, if your masterproject requires the booksubmodule, it is a nice gesture to keep it as such because that way other users who checkout your project can then enjoy not having any special gitcommand to run (well... there are some special commands to use submodules, but it still simpler to manage, overall, I think.)

只是,如果您的master项目需要book子模块,保留它是一个很好的姿态,因为这样其他签出您项目的用户就可以享受没有任何特殊git命令来运行(嗯......有一些特殊命令可以使用子模块,但总的来说,我认为管理起来仍然更简单。)

In your case you make changes in the bookrepository and at some point you commit those changes. This means you have new commitsin that submodule, which have a new SHA1 reference.

在您的情况下,您在book存储库中进行更改,并在某个时候提交这些更改。这意味着您在该子模块中有新的提交,其中有一个新的 SHA1 引用。

What you need to do in the master directory is commit those changes in the master repository.

您需要在主目录中做的是在主存储库中提交这些更改。

cd /path/to/master
git commit . -m "Update 'book' in master"

This will updated the SHA1 reference in masterto the newest version available in the bookrepository. As a result this commit allows others to checkout all of the master& bookrepositories at the tip.

这会将 SHA1 引用更新masterbook存储库中可用的最新版本。因此,此提交允许其他人在提示处检出所有master&book存储库。

So in effect you end up with one more commit whenever you make changes to a submodule. It is semi-transparent if you also make changes to some files in the masterrepository since you'd commit both at the same time.

因此,实际上,每当您对子模块进行更改时,您最终都会再提交一次。如果您还对master存储库中的某些文件进行更改,则它是半透明的,因为您将同时提交这两个文件。

回答by think2010

Run

git submodule update 

at the root level.

在根级别。