44

In a git repository, I have set up my .gitmodules file to reference a github repository:

[submodule "src/repo"]
    path = src/repo
    url = repourl

when I 'git status' on this repo, it shows:

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:   src/repo (new commits)

If I cd into src/repo and git status on repo, it says that there is nothing to commit.

Why is my top-level git repo complaining?

39

It's because Git records which commit (not a branch or a tag, exactly one commit represented in SHA-1 hash) should be checked out for each submodule. If you change something in submodule dir, Git will detect it and urge you to commit those changes in the top-level repoisitory.

Run git diff in the top-level repository to show what has actually changed Git thinks. If you've already made some commits in your submodule (thus "clean" in submodule), it reports submodule's hash change.

$ git diff
diff --git a/src/repo b/src/repo
index b0c86e2..a893d84 160000
--- a/src/repo
+++ b/src/repo
@@ -1 +1 @@
-Subproject commit b0c86e28675c9591df51eedc928f991ca42f5fea
+Subproject commit a893d84d323cf411eadf19569d90779610b10280

Otherwise it shows -dirty hash change which you cannot stage or commit in the top-level repository. git status also claims submodule has untracked/modified content.

$ git diff
diff --git a/src/repo b/src/repo
--- a/src/repo
+++ b/src/repo
@@ -1 +1 @@
-Subproject commit b0c86e28675c9591df51eedc928f991ca42f5fea
+Subproject commit b0c86e28675c9591df51eedc928f991ca42f5fea-dirty

$ git status
On branch 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)
  (commit or discard the untracked or modified content in submodules)

    modified:   src/repo (untracked content)

no changes added to commit (use "git add" and/or "git commit -a")

To update which commit records should be checked out for the submodule, you need to git commit the submodule in addition to committing the changes in the submodule:

git add src/repo
  • What if I don't want to add any commits in the main repo? My use case is - I have a main repository, then I have its wiki which is also a repository (as available in GitHub and Bitbucket). Now I have added the wiki as a submodule in a directory wiki. I do not want any of my changes from the wiki(i.e. the wiki directory) to reflect in my main/code repository. Should I just add the .gitmodules path in my .gitignore of the main repository? How should I go about it? – yadav_vi May 28 '16 at 7:46
  • 11
    In my case all I needed to do was update the submodule itself from the main repo. Something like: git submodule update src/repo – Agustín Amenabar Dec 9 '16 at 13:03
16

I just ran into this same class of problem, and I was able to use the solution offered by @AugustinAmenabar in the comments section of the accepted answer. My setup was a bit more complex, so I added the --recursive flag to bring all dependencies up to date.

git submodule update src/repo --recursive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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