0

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.

Anh-Tuan Mai
  • 1,129
  • 19
  • 36
  • 1
    My submodule is usually not on a branch, but on a detached HEAD on the commit that the root repo says it should have. Since I'm not on a branch, `git status` can't know what remote branch to compare with. – joanis Apr 05 '19 at 14:33
  • Hi Joanis, I am an ordinary git user. Thanks for you comment, should a submodule be independent? If a submodule does not on its own branch, the root repo will be overcharge by the number of branches? Like a sub repo A with 2 varies, an other sub repo with 3 varies will make a 6 branches root repo? – Anh-Tuan Mai Apr 05 '19 at 16:03
  • Each submodule will have its own branches, independent of the branches in the root repo. When you're working in a submodule and making commits in it, you'll be on a branch where you push your commits. In this case, `git status` will show the local branch and its status with respect to the remote one. However, if you're not working on a submodule but instead got it cloned or updated via `git submodule` commands, then you'll see it in a detached HEAD status. You can say `git checkout ` in the submodule to change that when you need to work in it. – joanis Apr 05 '19 at 19:56
  • By the way, instead of `git status`, I like `git log --all --decorate --graph --format=oneline`: that shows me where HEAD is with respect to all the branches. – joanis Apr 05 '19 at 19:57
  • But a question: does `git status` inside the branch show `On branch develop` or `HEAD detached at `? – joanis Apr 05 '19 at 19:59
  • My submodule has it own repo on gitlab. – Anh-Tuan Mai Apr 06 '19 at 22:13
  • Yes, submodules always have their own separate repos, that's how they work. – joanis Apr 07 '19 at 12:21

1 Answers1

1

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.

joanis
  • 10,635
  • 14
  • 30
  • 40