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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With