如何在 git 子模块中“提交”更改?

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

How do I "commit" changes in a git submodule?

gitgit-submodules

提问by Dylan Beattie

I have, in my naivety, set up a git submodule and treated it like a Subversion external - i.e. it's now full of changes that I've just realized haven't been committed or pushed anywhere.

我天真地设置了一个 git 子模块并将其视为外部 Subversion - 即它现在充满了我刚刚意识到尚未提交或推送到任何地方的更改。

Is there some easy way to commit/push the submodule changes back to the upstream repo? And what's the recommended technique in Git for doing simultaneous development on separate (but linked) repositories in this way?

是否有一些简单的方法可以将子模块更改提交/推送回上游存储库?以这种方式在单独的(但链接的)存储库上进行同步开发的 Git 中推荐的技术是什么?

回答by timdev

A submoduleis its own repo/work-area, with its own .gitdirectory.

一个子模块是它自己的repo/work-area,有它自己的.git目录。

So, first commit/pushyour submodule'schanges:

所以,首先commit/push你的子模块的变化:

$ cd path/to/submodule
$ git add <stuff>
$ git commit -m "comment"
$ git push

Then, update your main projectto track the updated version of the submodule:

然后,更新您的主项目以跟踪子模块的更新版本:

$ cd /main/project
$ git add path/to/submodule
$ git commit -m "updated my submodule"
$ git push

回答by VonC

Note that if you have committed a bunch of changes in various submodules, you can (or will be soon able to) push everything in one go(ie onepush from the parent repo), with:

请注意,如果你犯了一堆各种子模块的变化,你可以(或将很快就能到)推动一切一气呵成(即一个来自父回购推),有:

git push --recurse-submodules=on-demand

git1.7.11 ([ANNOUNCE] Git 1.7.11.rc1) mentions:

git1.7.11 ( [ANNOUNCE] Git 1.7.11.rc1) 提到:

"git push --recurse-submodules" learned to optionally look into the histories of submodules bound to the superproject and push them out.

" git push --recurse-submodules" 学会了选择性地查看绑定到超级项目的子模块的历史并将它们推出。

Probably done after this patchand the --on-demandoption:

可能在此补丁--on-demand选项之后完成:

--recurse-submodules=<check|on-demand|no>::

Make sure all submodule commits used by the revisions to be pushed are available on a remote tracking branch.

  • If checkis used, it will be checked that all submodule commits that changed in the revisions to be pushed are available on a remote.
    Otherwise the push will be aborted and exit with non-zero status.
  • If on-demandis used, all submodules that changed in the revisions to be pushed will be pushed.
    If on-demand was not able to push all necessary revisions it will also be aborted and exit with non-zero status.

确保要推送的修订使用的所有子模块提交在远程跟踪分支上可用。

  • 如果check使用,将检查在要推送的修订中更改的所有子模块提交是否在远程可用。
    否则推送将被中止并以非零状态退出。
  • 如果on-demand使用,则将推送所有在要推送的修订中发生更改的子模块。
    如果按需无法推送所有必要的修订,它也将中止并以非零状态退出。

This option only works for one level of nesting. Changes to the submodule inside of another submodule will not be pushed.

此选项仅适用于一层嵌套。不会推送对另一个子模块内的子模块的更改。

回答by rahvin_t

$ git submodule status --recursive

Is also a life saver in this situation. You can use it and gitk --allto keep track of your sha1's and verify your sub-modules are pointing at what you think they are.

在这种情况下也是救命稻草。您可以使用它并gitk --all跟踪您的 sha1 并验证您的子模块是否指向您认为的内容。

回答by nickgrim

You can treat a submodule exactly like an ordinary repository. To propagate your changes upstream just commit and push as you would normally within that directory.

您可以像对待普通存储库一样对待子模块。要向上游传播更改,只需像通常在该目录中一样提交和推送即可。

回答by fantastory

Before you can commit and push, you need to init a working repository tree for a submodule. I am using tortoise and do following things:

在提交和推送之前,您需要为子模块初始化一个工作存储库树。我正在使用乌龟并执行以下操作:

First check if there exist .git file (not a directory)

首先检查是否存在 .git 文件(不是目录)

  • if there is such file it contains path to supermodule git directory
  • delete this file
  • do git init
  • do git add remote path the one used for submodule
  • follow instructions below
  • 如果有这样的文件,它包含超级模块 git 目录的路径
  • 删除这个文件
  • 做 git init
  • do git add remote path 用于子模块的路径
  • 按照下面的说明

If there was .git file, there surly was .git directory which tracks local tree. You still need to a branch (you can create one) or switch to master (which sometimes does not work). Best to do is - git fetch - git pull. Do not omit fetch.

如果有 .git 文件,那么肯定会有跟踪本地树的 .git 目录。您仍然需要一个分支(您可以创建一个)或切换到 master(有时不起作用)。最好的做法是 - git fetch - git pull。不要省略 fetch。

Now your commits and pulls will be synchronized with your origin/master

现在您的提交和拉取将与您的原点/主站同步