0

I have a submodule sub in a Git project super. Now there are multiple branches of super and each may point to another commit of sub. Changing the branch of super via

>$ git checkout <branchname>

does not properly adapt sub to point to the correct commit again, though. Instead the result of

>$ git status

contains a modified entry for sub.

This is only one of several situations where one might want to revert any modifications of sub and checkout the commit to which super actually points.

For files you can always run

>$ git checkout -- path/to/file

to revert any modifications. So I am basically looking for an equivalent call to revert submodules in a similarly quick and simple manner.

I know this is possible by combination of two commands:

>$ git submodule deinit -f /path/to/sub
>$ git submodule update --init --recursive /path/to/sub

but I am looking for a shorter version which might be easier to remember and faster to type ;)

Do you have any recommendations?

Thargon
  • 424
  • 1
  • 3
  • 12
  • Possible duplicate of [Git hook for updating Git submodule on branch change](https://stackoverflow.com/questions/50176805/git-hook-for-updating-git-submodule-on-branch-change) – phd Jul 04 '18 at 12:46
  • You do not need to deinit and re-init, just `git submodule update`. Or, use Thiru's answer if your Git is new enough. – torek Jul 04 '18 at 15:56

2 Answers2

1

Could you please try this command and check?

git checkout --recurse-submodules {branch_name}

Thiru
  • 2,541
  • 4
  • 25
  • 39
  • That's probably the best method but requires Git version 2.13 or later (and 2.13.2 has a bug fix for submodules that contain their own submodules). – torek Jul 04 '18 at 15:55
  • I cannot test it as my version of Git is too old. However, I think this is not what I was looking for.I do not want to checkout a specific branch, but the commit the `super` points to, thereby dropping any local changes in `sub`. – Thargon Jul 17 '18 at 14:23
1

As mentioned by torek, skipping the deinit step works well. Thus the following command does the job for me:

git submodule update --recursive /path/to/submodule

Thank you all for your answers!

Thargon
  • 424
  • 1
  • 3
  • 12