Gitサブモジュールをoriginの最新コミットに更新する 質問する

Gitサブモジュールをoriginの最新コミットに更新する 質問する

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 log新しいgit showコミットの存在を拒否します。これまでは、rmモジュールを - して再度追加していましたが、これは原理的に間違っており、実際には面倒です。

ベストアンサー1

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

おすすめ記事