gitgit-submodules

Is it possible to use a different branch in a git submodule?


I have a repository that I am using which has three submodules. So when I cloned it I did git clone --recurse-submodules ProjectA.git

Among this "project A"'s submodules there is "Project B" which I found is faulty (some problem with its dockerfile etc- not really related to this question).

So I created a branch in ProjectB and I corrected its fault. Now I have tried it independently and it seems to work fine.

Of course the ideal thing would be to request to merge it to the main branch but I am not looking forward to it soon.

So, my question is , how can I make that ProjectA use ProjectB's FeatureBranch (that I created) in its submodules, so that it can run correctly?

Right now, I am assuming the submodules all use their main branch .


Solution

  • Submodules in Git are essentially pointers to specific commits in another repository. They don't automatically track branches, but you can set them to point to any commit on any branch you want, including your custom FeatureBranch.

    1. Navigate to the submodule directory
    cd ProjectA/ProjectB
    
    1. Fetch and checkout your feature branch
    git fetch origin
    git checkout FeatureBranch
    
    1. Go back to the root of ProjectA
    cd ..
    
    1. Update the submodule pointer to this commit
    git add ProjectB
    git commit -m "Update ProjectB submodule to FeatureBranch"
    
    1. Push the change to ProjectA's repo
    git push