2

I have a project parent with branches master and work. The master branch is solely used to pull changes from an upstream repository and from time to time merge these changes into the work branch. No changes will be done to the master branch.

The project has a submodule child. At some point, after I merged changes from the master branch to the work branch, I noticed that the submodule pointers on the two branches differ. Whenever I switch from one branch to the other and then type git status, I get, e.g. when switching back to master:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   child (new commits)

And the output for git diff is something along the lines of:

diff --git a/child b/child
index 02232f9..5902a22 160000
--- a/child
+++ b/child
@@ -1 +1 @@
-Subproject commit abcdefdeadbeef0123456789
+Subproject commit 12381257dea0ffff018dead

Updating the submodule via git submodule update "fixes" the immediate issue by making the change go away, but whenever I switch branches I will have a tainted index again. I want the submodule pointer of branch work to always point to the same commit as the one on branch master did when I last merged master into work, but after executing git submodule update, I can't merge master into work, because git thinks that work is already up-to-date.

How can I "repair" the submodule pointer in branch work and make it point to the same commit as the one in branch master does?

I know that there was a conflict issue once in the past with the submodule on branch work, but I don't know anymore when and where it happened and therefore I can't really tell why exactly this situation occurred.

Is there a way to find out which commit to parent changed the submodule pointer? According to this question, it's not possible?

Community
  • 1
  • 1
moktor
  • 1,011
  • 1
  • 8
  • 12

1 Answers1

1

You have to commit the updated sub-module just like you would a code change. After getting the submodule to the commit that you want it, from the root of the main project while in the branch you want to update (in your case work):

git add .
git commit -m "Updating Submodule"

That will make your branch "point" at the correct commit.

Alternatively, if you have other changes that you don't want to commit

git add /path/to/module
git commit -m "Updating Submodule"
TheGentleman
  • 2,324
  • 13
  • 17
  • This solved the problem when done on the `work` branch! It would be nice if you could clarify your answer, i.e. on branch `work`, you have to manually update the submodule to the specific commit you want it to be at and *then* add that change to the index! – moktor Jan 05 '17 at 13:52
  • 1
    I added some clarification. I left it more general for posterity sake since `git commit` always affects the branch you're currently on and sometimes someone might want to update a sub-module on a different branch. – TheGentleman Jan 05 '17 at 13:59