Search code examples
gitgit-submodulesgit-status

Git - in submodule 'git status' does not give "is up-to-date" info as in root repo


I have a git repo with a submodule. In the root repo, git status gives me the status of the branch in compare with remote branch. Eg:

On branch develop
Your branch is up-to-date with 'origin/develop'.

But in the submodule repo, I dont have the second line. How can I get this information in all submodules? Thanks.


Solution

  • When you work with Git submodules, the state of your submodules is going to be detached HEAD by default.

    When you run git submodule update, whether it's the first time or a new commit has been pushed by someone else for the submodules, Git will checkout the sha1 that the root project says that submodule should be at.

    E.g.

    > git submodule update
    > cd <submodule>
    > git status
    HEAD detached at 59fe3c5232
    nothing to commit, working directory clean
    

    Now, I can see more information by using git log:

    > git log --all --decorate --graph --format=oneline
    * 59fe3c5232 (HEAD, origin/master, master) Commit details
    * 8762eca8d9 Older commit
    ...
    

    When there are branches, that log will be more interesting: look for HEAD to see where you're at.

    With my example, if I now do git checkout master inside the submodule, I'm still on the commit expected by the root repo, but I'm tracking the master branch inside the submodule too.

    > git checkout master
    > git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    nothing to commit, working directory clean
    

    But if I were to checkout a commit that was not what the root repo expected, then the root repo would consider that a change that needs to be committed. (Do git diff or git status in the root repo to see that.)

    If someone else on the team pushes a commit to the submodule, the next time I update the root repo and do git submodule update, I'll end up back in detached head mode.

    I only bother to checkout a branch in the submodule if I'm actually working on that submodule.