thisisshantzz
thisisshantzz

Reputation: 1097

git status showing confusing results

I have two branches apart from master, say BR1 and BR2. Now, I did the following

git checkout BR1
git submodule add <submodule1_repo_url> path/to/submodule1
git submodule add <submodule2_repo_url> path/to/submodule2
git add path/to/submodule1
git add path/to/submodule2
git commit -m "Some commit msg"

When I do a git status now, I see that my changes are committed and that there is nothing to commit.

Now, when I switch over to BR2 and do a git status, I see that path/to/submodule1 and path/to/submodule2 are shown as untracked files. The point is that I added these submodules in BR1. Why are they showing up as untracked in BR2 and how do I make sure they don't come up as untracked in BR2 because I don't want to commit these submodules under BR2.

Any help would be appreciated.

EDIT: Output of git status on BR1

On branch BR1
nothing to commit, working tree clean

Output of git status on BR2

On branch BR2
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    path/to/submodule1/
    path/to/submodule2/

nothing added to commit but untracked files present (use "git add" to track)

Upvotes: 0

Views: 43

Answers (2)

phd
phd

Reputation: 94397

If you have a few branches that have different set of submodules there are perhaps two ways to handle them.

Variant 1: a post-checkout hook that handle submodules. Something like this (I really have such a hook):

#!/bin/sh

# post-checkout hook that switches submodules

prev_HEAD="$1"
new_HEAD="$2"
new_branch="$3"

if [ "$new_branch" = 1 ]; then
   if ! cat .gitmodules | grep -Fq SUBMODULE1; then
      rm -rf SUBMODULE1 SUBMODULE2
   fi

   git submodule update
fi

exit 0

Variant 2: worktrees. My git is a bit old so I don't use git worktree. I just created clones, checked out long-lived branches and never checkout different branches in the clones.

Upvotes: 1

larsks
larsks

Reputation: 311288

git does not delete the contents of the submodule directories when you switch branches. They show up as untracked because you have not added them to BR2. You can safely ignore them.

You only see the top-level submodule directory instead of its contents because they are git repositories (they contain a .git directory), so git elides their content on the theory that you wouldn't try adding it directly to the parent repository in any case.

Upvotes: 2

Related Questions