3

We have the main git repository and one single git submodule. For this question, the main repository does only have a master branch, and the submodule does have two branches - X and Y.

Let's say Coder A just switched to submodule branch X and applied some small changes.
Coder B it's on submodule branch Y.

Now A pushes to the remote and B pulls - then it does not seem like B's active submodule commit (on their local machine) gets automatically changed to the commit that A pushed on branch X, even though A actively committed the changed submodule commit.
Instead for B, their version on branch Y stays active.

If B does now manually pull from the repository and set the branch to X, everything works again.

How can we achieve that submodule changes / active commits get automatically synced when pulling from the main repository?

We're using Source Tree as Git GUI if that makes any explanations easier.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
keyboard
  • 1,655
  • 1
  • 14
  • 24

1 Answers1

2

then it does not seam like B's active submodule commit (on their local machine) gets automatically changed to the commit that A pushed on branch X, even though A actively committed the changed submodule commit

You need to be sure A committed and push from the submodule, and then add/commit and push from the parent repo (in order to record the new submodule SHA1)

Since Git 1.7.5, git pull should update the submodules as well.
Double-check with a git submodule update --init --recursive

But don't forget that "updating a submodule" would only check out its SHA1, not a branch. Unless a submodule is set to track a branch, and you do a git submodule update --recursive --remote.
Even then, it would pull one branch (X or Y), not both.

A friend just told us that the repo on my friends machine might be broken. So we re-cloned it from Origin and now everything just works.

I'm now looking into tracking branches with submodules, to avoid the "checked out HEAD" issue. Can you recommend that?

As explained in the "True Nature of Submodules", a submodule is always in a detached HEAD mode at first.
You can still force it to follow a branch and update itself: see "git submodule tracking latest".
That means the following command would update the submodule content after pulling a branch said submodule must follow:

git submodule update --remote
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thanks, I'm not sure if I understand completely though. I pulled an SHA-1 change from the parent repo, but it seemed like the code has not changed / was still the same as before. Is a pull from parent repo with new SHA-1 for submodule enough to check out the right commit on the submodule? That checked out commit should then also be visible in Git / Source Tree of the submodule, right? – keyboard Jul 05 '17 at 12:39
  • @keyboard no: the SHA1 you pull in the parent is a gitlink. See https://stackoverflow.com/a/32327200/6309. Within a submodule, you should see that same SHA1 as (detached) HEAD. – VonC Jul 05 '17 at 12:54
  • Sorry, I've clicked and read through your posts, I still don't understand what a git link is, could you elaborate? What's the difference between pulling a git link SHA-1 and checking out a special commit with a certain SHA-1? – keyboard Jul 05 '17 at 16:14
  • I just did the following test with a coworker: I changed a string in the parent repo and a string in the submodule repo. Then I committed and pushed both. My coworker then pulled from the main repo, Source Tree showed my coworker the new commit SHA-1 of the submodule repo as the "subproject commit" in the commit details, but this commit is not checked out in the submodule repo (hence the code has not changed, and the changed strings are not on my coworker's machine). Only by manually pulling from the submodule repo, the strings get changed. What are we doing wrong? – keyboard Jul 06 '17 at 13:48
  • You need to add the gitlink as well: commit first in the submodule (and push), then add, commit and push to include the glitlink. (https://stackoverflow.com/a/16581096/6309, https://stackoverflow.com/a/19354410/6309) – VonC Jul 06 '17 at 13:50
  • I still don't fully understand what a gitlink is, nor how I could add it (even though again reading your links). Whatever a gitlink is, or how I add it - would I have to do that every single time I commit then? Sorry, I'm completely lost. I've used git for years successfully, but I'm completely unable to understand this. – keyboard Jul 06 '17 at 16:08
  • @keyboard it is the SHA1 of the submodule repo: each time you modify the submodule, the parent repo must record the new SHA1 of that submodule repo in order to be able to checkout it at the right SHA1 next time. Do read "The true nature of submodules": https://stackoverflow.com/a/1979194/6309 – VonC Jul 06 '17 at 16:10
  • Ah...so it's just the SHA-1 that changed, and that is shown to me as a change on the main repo. I did indeed commit and push this, also my coworker pulled this. So on my coworker's machine it showed a change to the submodule's SHA-1 (this is the gitlink, right?). But still it did not work. – keyboard Jul 06 '17 at 16:22
  • @keyboard Can your coworker do a git submodule update --init? – VonC Jul 06 '17 at 16:26
  • we just tried - nothing changed. We then did the procedure again afterwards (in case things might change the next time), still the same issue though. – keyboard Jul 06 '17 at 18:20
  • I am comutting right now. What a git status returns when done in the submodule? – VonC Jul 06 '17 at 18:52
  • Sorry, I'm so confused that I don't even understand your question ;) – keyboard Jul 06 '17 at 21:25
  • @keyboard When your colleague does a `git pull` in the parent repo, followed by a `git submodule update --init`, what does, *inside* the submodule of the colleague's repo, return a `git status` and `git log`: do you see the expected commit? (the one you added and pushed from your side) – VonC Jul 06 '17 at 22:04
  • Here's the output, we can't see the expected commit: https://pastebin.com/GWHw84zU – keyboard Jul 08 '17 at 13:44
  • @keyboard Did you push that commit (with the Subproject commit updated). Did your colleague pull that commit (does a git log in the parent repo shows that commit) – VonC Jul 08 '17 at 19:17
  • You know what? A friend just told us that the repo on my friends machine might be broken. So we re-cloned it from Origin and now everything just works. I can't believe it myself though. I'm now looking into tracking branches with submodules, to avoid the "checked out HEAD" issue. Can you recommend that? – keyboard Jul 08 '17 at 21:30
  • Thanks, I will mark as correct now, things are working with the head. I quickly tried to get the tracking to work, but it didn't seem to do what I want and I'm currently too exhausted of git to keep trying ;) – keyboard Jul 08 '17 at 21:50