I have a project named sample which have 2 submodules sampleA and sampleB. I also have another git repo named Example which also refers to same submodule sampleA and sampleB. When I change something in submodule sampleA from the project Example and pushed into remote. Now the submodule sampleA from the project sample pointing to old commit of the submodule sampleA. I want all of these submodules to point to latest commit.

my .gitmodules:

[submodule "sampleA"]
    path = sampleA
    url = https://gitlab....sampleA.git
    branch = alpha
[submodule "sampleB"]
    path = sampleB
    url = https://gitlab...sampleB.git
    branch = alpha

I have many branches. I need to point the submodule to latest commit of corresponding branch in every project. When i go throw the internet for the solution found solutions similar to this:

Update Git submodule to latest commit on origin

that is to update manually through command ,but i want to do this automatically for remote. otherwise i need to push this to all of my projects. How can i do this?

3 Replies 3

Open-ended questions are questions nonetheless. I don't see a question here. Neither in title, nor in body. (Edit: I see your "How can I do this" addition. Minimal, but technically a question, okay.)

Running git submodule update --remote (or equivalent commands) is the only way. Even worse, after updating submodules you should go back to the superproject repo and commit changes in submodules. You can semiautomate this using scripts, running from cron or a CI pipeline but said commands must be run somehow, no way around that.

After few days of research got solution which need less effort for the submodule handling as it follows:

I have added a Git hook configuration in our project sample. This setup will automatically update sub modules whenever :

* switch branches ( git checkout)
* pull new changes (git pull)

This ensures submodules like sampleA and sampleB always point to the latest commit and remain consistent across all projects.

One-Time Setup Steps

1. Pull the sample repository

2. Run the following commands from the project root:

a) Configure global Git hooks path

       git config --global core.hooksPath .githooks

On Linux, make the hook files executable:

   chmod +x .githooks/*

Windows users can skip this step because Git on Windows does not require executable permissions for hook scripts.You can simply proceed without running that command.
Verify the config:

      git config core.hooksPath

Expected output:
.githooks

b) Add alias for git pull with automatic submodule update

 git config alias.pull '!git pull --ff-only && git submodule update --remote --recursive'

Verify:

    git config --get alias.pull  

Expected output:
!git pull --ff-only && git submodule update --remote --recursive

Once this setup is complete:

* Submodules will stay automatically in sync
* No need to use `—recurse-submodules` you can simple use git pull
* No need to manually checkout/pull submodules (git checkout alpha from the root will also checkout the submodule to the respective branch)
* You will always work with the latest submodule revisions

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.