Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git checkout commit and have submodules update to that commit?

Tags:

I'm cloning a project with submodules and reverting to a commit from about a year ago:

$ git clone --recursive --branch 5.6 https://codereview.qt-project.org/pyside/pyside-setup
$ cd pyside-setup
$ git checkout 8913156381b7dc51f903b9e459c143fb25097cab

M   sources/pyside2-examples
M   sources/pyside2-tools
Note: checking out '8913156381b7dc51f903b9e459c143fb25097cab'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 89131563... Adding sync.profile file for Qt CI usage

If I go down into one of the git submodules, they are empty:

$ ls -alh sources/pyside2

total 0
drwxr-xr-x  2 fredrik  staff    68B Aug 11 00:12 ./
drwxr-xr-x  7 fredrik  staff   238B Aug 11 00:12 ../

What's the best approach to checkout the submodules as they were at the time of commit 8913156381b7dc51f903b9e459c143fb25097cab (of the main project)?

In this particular case, I only have two submodules. But what if the project had many more submodules?

EDIT #1: I'm on Git 2.14.1.

What I tried to do, but without success

I can view the main project's commit details at 8913156381b7dc51f903b9e459c143fb25097cab in the git web viewer, here. If I click the "tree" link (next to the "tree" hash), I can view the project at that commit. Traversing down to submodules sources/pyside2 and sources/shiboken2, I see that they were both at commit c764273e64896215730e44eb907cd3535596ade4 at that time.

But if I manually try to checkout those submodules, their directories are still completely empty:

$ cd sources/pyside2
$ git checkout c764273e64896215730e44eb907cd3535596ade4
HEAD is now at c764273e... Fix OS/X inclusion of framework headers.

$ ls -alh
total 0
drwxr-xr-x  2 fredrik  staff    68B Aug 11 00:12 .
drwxr-xr-x  7 fredrik  staff   238B Aug 11 00:12 ..
like image 440
fredrik Avatar asked Aug 10 '17 22:08

fredrik


People also ask

How can I update one submodule?

In order to update an existing Git submodule, you need to execute the “git submodule update” with the “–remote” and the “–merge” option. Using the “–remote” command, you will be able to update your existing Git submodules without having to run “git pull” commands in each submodule of your project.

How do submodules work in git?

A git submodule is a record within a host git repository that points to a specific commit in another external repository. Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

What is git submodule sync?

git submodule sync synchronizes all submodules while git submodule sync -- A synchronizes submodule "A" only. If --recursive is specified, this command will recurse into the registered submodules, and sync any nested submodules within.


1 Answers

Try at least:

cd /path/to/main/project
git submodule update --init --recursive

That should be able to clone your submodule at the right SHA1

like image 193
VonC Avatar answered Sep 23 '22 20:09

VonC