Please describe the problem.

Consider a parent repository with a single submodule and two branches in the parent repository. When checking out a branch in the parent using --recurse-submodules, git returns the error:

fatal: could not open 'sub/.git' for writing: Is a directory

This issue was originally mentioned in this post where there is a statement that "The conversion of .git file to .git symlink mostly won't bother git." It is precisely this change that git has a problem with when changing to a new branch. The original poster said there was a solution to use "git checkout mybranch && git submodule update", but this does not work with git version 2.40.0 and git-annex version 10.20230407. In this case, rather than giving a fatal error, git is unable to remove the submodule directory and leaves it untouched. However, this then leaves the new branch in a dirty state (because the submodule is an untracked or modified file).

The end goal for using --recurse-submodules is so that either 1) a submodule will exist only in a single branch and/or 2) different branches in the parent will automatically point to different branches in the submodule

What steps will reproduce the problem?

Here is a sequence of steps to reproduce the issue.

# create git repo to use as a submodule
if [ -d $SUB_DIR ]; then
    sudo rm -rf $SUB_DIR
mkdir -p $SUB_DIR
git init
git annex init
# add file to main
touch sub_file1.txt
git annex add .
git commit -m "Add sub_file1.txt"

# setup parent dataset
if [ -d $PRNT_DIR ]; then
    sudo rm -rf $PRNT_DIR
mkdir -p $PRNT_DIR
git init
git annex init
# add file to main
touch file1.txt
git annex add .
git commit -m "Create file1.txt"

# Add branch 1 to parent dataset
git checkout -b branch_1 main
# add submodule 1 to branch 1 of parent dataset
git -c protocol.file.allow=always submodule add $SUB_DIR/.git sub
git commit -m "Add submodule 1"

# add new file to submodule annex
cd sub
touch sub_file2.txt
git annex add .
git commit -m "Create sub_file2.txt"

# register changes in the parent
cd ..
git add .
git commit -m "Register sub changes"

# return to main branch, this fails
git checkout main --recurse-submodules
# return to main branch, this fails
# git checkout main && git submodule update

What version of git-annex are you using? On what operating system?

git version 2.40.0

git-annex version: 10.20230407

operating system: darwin aarch64

Have you had any luck using git-annex before? (Sometimes we get tired of reading bug reports all day and a lil' positive end note does wonders)

Yes! Been familiarizing myself with it on and off over the last year and am very excited about it's possibilities.