git - 将 Git 子模块更新为源上的最新提交

标签 git git-submodules git-pull

我有一个带有 Git 子模块的项目。它来自 ssh://... URL,并且在提交 A 上。提交 B 已被推送到该 URL,我希望子模块检索提交并对其进行更改。

现在,我的理解是 git submodule update 应该这样做,但事实并非如此。它什么都不做(没有输出,成功退出代码)。这是一个例子:

$ mkdir foo
$ cd foo
$ git init .
Initialized empty Git repository in /.../foo/.git/
$ git submodule add ssh://user@host/git/mod mod
Cloning into mod...
user@host's password: hunter2
remote: Counting objects: 131, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 131 (delta 54), reused 0 (delta 0)
Receiving objects: 100% (131/131), 16.16 KiB, done.
Resolving deltas: 100% (54/54), done.
$ git commit -m "Hello world."
[master (root-commit) 565b235] Hello world.
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 mod
# At this point, ssh://user@host/git/mod changes; submodule needs to change too.
$ git submodule init
Submodule 'mod' (ssh://user@host/git/mod) registered for path 'mod'
$ git submodule update
$ git submodule sync
Synchronizing submodule url for 'mod'
$ git submodule update
$ man git-submodule 
$ git submodule update --rebase
$ git submodule update
$ echo $?
0
$ git status
# On branch master
nothing to commit (working directory clean)
$ git submodule update mod
$ ...

我也尝试过 git fetch mod,它似乎可以进行抓取(但不可能,因为它不提示输入密码!),但是 git loggit show 否认新提交的存在。到目前为止,我一直在rm-ing 模块并重新添加它,但这在原则上是错误的,在实践中也是乏味的。

最佳答案

git submodule update命令实际上告诉 Git 你希望你的子模块每次检查已经在 super 项目的索引中指定的提交。如果您想将您的子模块更新到远程可用的最新提交,您需要直接在子模块中执行此操作。

总结一下:

# Get the submodule initially
git submodule add ssh://bla submodule_dir
git submodule init

# Time passes, submodule upstream is updated
# and you now want to update

# Change to the submodule directory
cd submodule_dir

# Checkout desired branch
git checkout master

# Update
git pull

# Get back to your project root
cd ..

# Now the submodules are in the state you want, so
git commit -am "Pulled down update to submodule_dir"

或者,如果您是一个忙碌的人:

git submodule foreach git pull origin master

关于git - 将 Git 子模块更新为源上的最新提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5828324/

相关文章:

git - 无法想到 git diff master..lab 和 git diff master...lab 会不同的情况

git - .gitignore 排除目录中的文件但不排除某些目录

git (ls-remote) - 列出按创建日期排序的 "remote"分支

linux - 在 git init 上自动添加 Remote

Git 子模块未在 Jenkins 构建中更新

子模块中的 Git Merge 冲突。如何提交他们的版本?

git - “git fetch --tags --force”和 “git pull <branch>”是可交换的操作吗?

eclipse - 如何在 Eclipse 的 eGit 的子模块内提交更改?

git - 如何自动保持 2 个 git 存储库同步

git - git pull 和 git fetch + git rebase 有什么区别?