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

2

5

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?

Wynnie answered 11/4, 2019 at 11:10 Comment(0)
B
3

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.

Blatt answered 11/4, 2019 at 13:4 Comment(4)
I had not made the hook script executable. After doing that - the hook works.Wynnie
You might want to run git config advice.ignoredHook true in that case. It'll turn on a warning when a hook isn't run because it's not executable, so you won't run into that problem (in this repo at least) again.Blatt
TBH, if I know enough to set that config parameter I can probably get the hook right anyway...Wynnie
It didn't show up until git 2.16, so you'd need to update your git anyway, and if you do that it should be turned on by default.Blatt
B
5

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.

Brazee answered 11/4, 2019 at 11:30 Comment(0)
B
3

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.

Blatt answered 11/4, 2019 at 13:4 Comment(4)
I had not made the hook script executable. After doing that - the hook works.Wynnie
You might want to run git config advice.ignoredHook true in that case. It'll turn on a warning when a hook isn't run because it's not executable, so you won't run into that problem (in this repo at least) again.Blatt
TBH, if I know enough to set that config parameter I can probably get the hook right anyway...Wynnie
It didn't show up until git 2.16, so you'd need to update your git anyway, and if you do that it should be turned on by default.Blatt

© 2022 - 2024 — McMap. All rights reserved.