Can I make git automatically update submodules when checking out a branch?

▼魔方 西西 提交于 2021-02-05 05:34:05

问题


I'm working on a git repository with some submodules, which have different revisions on different branches. When I switch branches, I get a:

M          path/to/subrepo
Switched to branch 'another-branch'

message. I then manually do:

git submodule update --recursive

and that goes away.

I tried writing a git hook, in .git/hooks/post-checkout:

#!/bin/bash

exec git submodules update --recursive

but this doesn't seem to do anything. I tried to add an exec echo hello from post-hook line - doesn't seem to work either.

My question: Can I configure git so that the branch checkout itself will also update the submodules, by default?


回答1:


I tried writing a git hook, in .git/hooks/post-checkout:

Check that you've spelled the name of the hook correctly and that the hook has the executable flag (git should give you a warning if it doesn't).

You've got a typo in the script you posted: there should be no s at the end of submodule. Maybe that's just an error in what you posted, but if it's in your actual hook you should see an error message when the hook runs, and the fact that you don't (and also that your echo doesn't work) suggests that the hook isn't running at all.

If you don't have the typo mentioned above and if echo statements in your hook do work, then it's not surprising that you don't see your git submodule update line doing anything — that command doesn't give any output if the submodules in your project already match the commits that are specified in the branch. The only time you'll see output is if there's a submodule that actually needs to be updated. Also remember that git submodule update doesn't get the latest versions of the submodules, it gets the ones that you've committed in your project.

In general, you've got the right idea: you can definitely add a hook called post-checkout to .git/hooks/, and it should run whenever you successfully git checkout some branch.




回答2:


If your git version is 2.13 or later, try the option --recurse-submodules:

git checkout another-branch --recurse-submodules

In some situations, you might need add -f too.



来源:https://stackoverflow.com/questions/55631116/can-i-make-git-automatically-update-submodules-when-checking-out-a-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!