None of the answers here solve my problem.
I am documenting/sharing here what worked for me. Hoping it helps someone else.
Initially my submodule was at commit A (at the time of adding submodule to main repo), then I checked out a branch (let us call it new-submodule-branch
) and made commits B and C to it and pushed it to remote (github.com)
Post this, my main repo started showing
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: <submodule_name> (new commits)
If, from the main repo root, I ran git submodule update --remote --init --recursive
, it kept reverting my submodule's HEAD in detached state to commit A
So then I set the branch value as new-submodule-branch
in <MainRepo>/.gitmodules
as follows
[submodule "<submodule_name>"]
path = <submodule_name>
url = [email protected]:ProProgrammer/<submodule_name>.git
branch = new-submodule-branch
post this, when I ran git submodule update --remote --init --recursive
, it would no longer revert my submodule's HEAD in detached state to commit A however it still kept showing the annoying
modified: <submodule_name> (new commits)
So far, I was following the official git reference for submodules, now I decided to do some more Googling, and I stumbled an article titled Getting git submodule to track a branch, this clearly said
You have to go and update that submodule commit reference to the latest code in the remote branch to avoid this
So finally, I did what I was trying to avoid:
git add <submodule_name>
git commit --amend --no-edit # I combined this with the previous commit where I added the 'branch' value in .gitmodules
If you want to see what this looks like once pushed to remote (github.com in my case), you can see this exact commit here