将Git子模块更新为最新的原始提交

我有一个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你希望你的子模块检查已经在超级项目的索引中指定的提交。 如果要将子模块更新为可从其远程获得的最新提交,则需要直接在子模块中执行此操作。

总之:

# 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 1.8.2提供了一个新选项--remote ,可以准确地实现这种行为。 运行

git submodule update --remote --merge

将从每个子模块的上游获取最新更改,并将其合并,并检查子模块的最新版本。 正如文件所说:

- 远程

该选项仅对更新命令有效。 使用子模块的远程跟踪分支的状态,而不是使用超级项目的已记录的SHA-1来更新子模块。

这相当于在每个子模块中运行git pull ,这通常正是您想要的。


在你的项目父目录下运行:

git submodule update --init 

或者如果您有递归子模块运行:

git submodule update --init --recursive

有时这仍然不起作用,这是因为在子模块更新时,您在本地子模块目录中进行了本地更改。

大多数情况下,本地更改可能不是您想要提交的更改。 这可能是由于子模块中的文件删除等原因造成的。如果是,请在您的本地子模块目录中和项目父目录中重新运行:

git submodule update --init --recursive 
链接地址: http://www.djcxy.com/p/16427.html

上一篇: Update Git submodule to latest commit on origin

下一篇: Changing remote repository for a git submodule