git 将子模块更新为最新提交

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

Update a submodule to the latest commit

gitgit-submodules

提问by fat

I have a project A which is a library and it is used in a project B.

我有一个项目 A,它是一个库,用于项目 B。

Both projects A and B have a separate repository on github BUT inside B we have a submodule of A.

项目 A 和 B 在 github 上都有一个单独的存储库,但在 B 内部,我们有一个 A 的子模块。

I edited some classes on the library, which is in the repo A, I pushed on the remote repo, so the library (repo A) is updated.

我在库中编辑了一些类,它位于 repo A 中,我推送了远程 repo,因此更新了库(repo A)。

These updates do not reflect on the "reference" (the submodule) the submodule refers to a previous commit.... what should I do in order to update the submodule on git?

这些更新不会反映在“引用”(子模块)上,子模块指的是以前的提交……我应该怎么做才能在 git 上更新子模块?

回答by Kjuly

Enter the submodule directory:

进入子模块目录:

cd projB/projA

Pull the repo from you project A (will notupdate the git status of your parent, project B):

从您的项目 A 中拉取 repo(不会更新您的父项目 B 的 git 状态):

git pull origin master

Go back to the root directory & check update:

返回根目录并检查更新:

cd ..
git status

If the submodule updated before, it will show something like below:

如果子模块之前更新过,它将显示如下内容:

# Not currently on any branch.
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#       modified:   projB/projA (new commits)
#

Then, commit the update:

然后,提交更新:

git add projB/projA
git commit -m "projA submodule updated"


UPDATE

更新

As @paul pointed out, since git 1.8, we can use

正如@paul 指出的那样,从 git 1.8 开始,我们可以使用

git submodule update --remote --merge

to update the submodule to the latest remote commit. It'll be convenient in most cases.

将子模块更新为最新的远程提交。大多数情况下会很方便。

回答by Paul Hatcher

Since git 1.8 you can do

由于 git 1.8 你可以做

git submodule update --remote --merge

This will update the submodule to the latest remote commit. You will then need to commit the change so the gitlink in the parent repository is updated

这会将子模块更新为最新的远程提交。然后,您需要提交更改,以便更新父存储库中的 gitlink

git commit

And then push the changes as without this, the SHA-1 identity the pointing to the submodule won't be updated and so the change won't be visible to anyone else.

然后推送更改,因为没有这个,指向子模块的 SHA-1 身份将不会更新,因此其他任何人都看不到更改。

回答by Adam Dymitruk

If you update a submodule and commit to it, you need to go to the containing, or higher level repo and add the change there.

如果您更新子模块并提交给它,您需要转到包含或更高级别的存储库并在那里添加更改。

git status

will show something like:

将显示如下内容:

modified:
   some/path/to/your/submodule

The fact that the submodule is out of sync can also be seen with

子模块不同步的事实也可以通过

git submodule

the output will show:

输出将显示:

+afafaffa232452362634243523 some/path/to/your/submodule

The plus indicates that the your submodule is pointing ahead of where the top repo expects it to point to.

加号表示您的子模块指向顶级存储库期望它指向的位置。

simply add this change:

只需添加此更改:

git add some/path/to/your/submodule

and commit it:

并提交它:

git commit -m "referenced newer version of my submodule"

When you push up your changes, make sure you push up the change in the submodule first and then push the reference change in the outer repo. This way people that update will always be able to successfully run

上推更改时,请确保先上推子模块中的更改,然后再推送外部 repo 中的引用更改。这样,更新的人将始终能够成功运行

git submodule update

More info on submodules can be found here http://progit.org/book/ch6-6.html.

关于子模块的更多信息可以在这里找到http://progit.org/book/ch6-6.html

回答by Andy Webov

Single line version

单行版本

git submodule foreach "(git checkout master; git pull; cd ..; git add '$path'; git commit -m 'Submodule Sync')"

回答by XtraSimplicity

A few of the other answers recommend merging/committing within the submodule's directory, which IMO can become a little messy.

其他一些答案建议在子模块的目录中合并/提交,IMO 可能会变得有点混乱。

Assuming the remote server is named originand we want the masterbranch of the submodule(s), I tend to use:

假设远程服务器已命名origin并且我们想要master子模块的分支,我倾向于使用:

git submodule foreach "git fetch && git reset --hard origin/master"

git submodule foreach "git fetch && git reset --hard origin/master"

Note: This will perform a hard reset on each submodule -- if you don't want this, you can change --hardto --soft.

注意:这将对每个子模块执行硬重置——如果您不想要这样,您可以更改--hard--soft.

回答by AnneTheAgile

My project should use the 'latest' for the submodule. On Mac OSX 10.11, git version 2.7.1, I did not need to go 'into' my submodule folder in order to collect its commits. I merely did a regular

我的项目应该为子模块使用“最新”。在 Mac OSX 10.11 的 git 版本 2.7.1 上,我不需要“进入”我的子模块文件夹来收集它的提交。我只是做了一个普通的

git pull --rebase 

at the top level, and it correctly updated my submodule.

在顶层,它正确更新了我的子模块。

回答by Miguel Fernandes Muldy

Andy's response worked for me by escaping $path:

安迪的回应通过转义 $path 对我有用:

git submodule foreach "(git checkout master; git pull; cd ..; git add $path; git commit -m 'Submodule Sync')"