13

git submodule update has supported the --depth option as described in this answer.

But still we can't easily determine the depth value, which would probably make git unable to find the intended revision of a submodule.

Is there a true solution for updating submodules shallowly?

3
  • 1
    It shouldn't prevent git to fetch the right depth. But if it was, then same comment as in stackoverflow.com/questions/24294361/…: (for git 1.9.x+) Did you try a git fetch --update-shallow in it (through a custom command/update maybe: stackoverflow.com/a/17693008/6309).
    – VonC
    Commented Jun 19, 2014 at 10:02
  • I would first test the git submodule update --depth first: it should be able to update to the right depth, depending on recorded SHA1.
    – VonC
    Commented Jun 19, 2014 at 10:03
  • As of git 2.0.0, --depth must be followed by a value. So I think the exact depth would still be a myth, which is the only and must be resolved concern of the question. Thanks for help @VonC
    – Bohr
    Commented Jun 19, 2014 at 12:04

2 Answers 2

6

So I think the exact depth would still be a myth, which is the only and must be resolved concern of the question

While there is indeed no "exact depth", you can still record a "depth recommendation" with git 2.9.x+ (Q3 2016).

See commit abed000, commit 37f52e9 (26 May 2016) by Stefan Beller (stefanbeller).
(Merged by Junio C Hamano -- gitster -- in commit 3807098, 20 Jun 2016)

submodule update: learn --[no-]recommend-shallow option

An upstream project can make a recommendation to shallowly clone some submodules in the .gitmodules file it ships.

Sometimes the history of a submodule is not considered important by the projects upstream.
To make it easier for downstream users, allow a boolean field 'submodule.<name>.shallow' in .gitmodules, which can be used to recommend whether upstream considers the history important.

This field is honored in the initial clone by default, it can be ignored by giving the --no-recommend-shallow option.

That way, a simple git submodule update (no additional parameters) will use the recommended depth value, if found.

See also "Git submodule without extra weight" with:

git config -f .gitmodules submodule.<name>.shallow true

g19fanatic proposes in the comments:

quick 1-liner to add shallow=true to all submodules:

git submodule | awk '{print $2}' | \
  xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true'

or, with git submodule foreach:

git submodule foreach 'git config -f .gitmodules submodule.$sm_path.shallow true'

However, akhan adds in the comments:

The foreach command is executed from the submodules and hence will not work as described here (try git submodule foreach 'echo $PWD' for instance).

However the xargs works just fine.

Working recipe:

git submodule | awk '{print $2}' | \
 xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true' 
git submodule update --depth=1
7
  • quick 1-liner to add shallow=true to all submodules git submodule | awk '{print $2}' | xargs -n 1 -I %% bash -c 'git config -f .gitmodules submodule.%%.shallow true'
    – g19fanatic
    Commented Sep 22, 2020 at 11:45
  • @g19fanatic Thank you. That should come in handy indeed. I have included your comment in the answer for more visibility.
    – VonC
    Commented Sep 22, 2020 at 11:54
  • @g19fanatic But I believe using git submodule foreach is safer and does not need to rely on awk and other parsing tricks.
    – VonC
    Commented Sep 22, 2020 at 11:55
  • probably right. never really used foreach but awk/xargs are in my utility belt. for completeness git submodule foreach 'git config -f .gitmodules submodule.$sm_path.shallow true'
    – g19fanatic
    Commented Sep 22, 2020 at 12:29
  • @g19fanatic And yet, as they say: "This is the way".
    – VonC
    Commented Sep 22, 2020 at 12:34
4

As of early 2024 the following command is accepted by git 2.43.0:

git submodule update --init --depth 1
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.