Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git submodule - use develop branch?

In my project repo, i have created a submodule and in the .submodules file it look like this:

[submodule "lib/payment-sdk"]
    path = lib/payment-sdk
    url = https://[email protected]/user/Payment-SDK.git 

I initialize the Submodules with the following commend: git submodule update --init --recursive but how to get the latest from the develop branch? On the production server it will need to pull from the master branch.

like image 705
I'll-Be-Back Avatar asked Apr 19 '26 17:04

I'll-Be-Back


1 Answers

The goal of a submodule is to get a specific version, not just "the latest"

What happens if the latest version was updated five minutes ago to an incompatible version? Your main module can't use that version. So that's not what submodules do. When using submodules, you pick a particular commit in the superproject. The superproject says, in effect: for the current superproject commit, use commit XYZ for submodule lib/payment-sdk. The submodule Git dutifully checks out that particular commit, as directed by the superproject git (git submodule update).

Setting the branch of the submodule does not change the above. See footnote 1 for more about the use of the branch name.

Now, if you're working in your repository, and you notice that there's a new commit for the submodule and you'd like to test it, you can do that quite easily. Just enter the submodule—it's a separate repository, after all—and git checkout the commit you want to test.1 The submodule remains on that commit while you build and test in the superproject. If all goes well, then, in the superproject, you run:

git add lib/payment-sdk

to tell the superproject that in the next commit you make, you'd like the superproject to command the submodule to use the same commit it's using right now.

If that's the only update you need to make to the superproject, you can now go ahead and commit:

git commit

Otherwise—e.g., if the new lib/payment-sdk requires some fixes or updates to the superproject—make any other superproject changes you need to make, git add them as well, and then git commit to make the new commit.


1You can also automate this a bit, using git submodule update --remote, but the details get rather complicated. I personally prefer, in most cases, to take direct control of each submodule by navigating into the submodule. If you do want to use git submodule update --remote, this is when the branch setting for the submodule means something.

At this point, you must now choose one of the three specific modes:

  • checkout: this means switch to a commit. The hash ID of the commit to check out in the submodule is determined by the branch name.

  • merge: this means merge the current commit with another commit. The hash ID of the commit to use for the merge is determined by the branch name.

  • rebase: this means rebase the current commit onto another commit. The hash ID of the commit to use for the rebase is determined by the branch name.

In all cases, git submodule will first run git fetch in the submodule repository, unless you inhibit this with --no-fetch or -N. The git fetch operation is what obtains new commits and updates the name, so that the branch name you set earlier takes on a new hash ID value after the git fetch. Remember that it is git fetch that obtains new commits and updates remote-tracking names like origin/master. This git fetch must be run in the submodule Git, not the superproject Git: these are after all separate, mostly-independent Git repositories. So the all-in-one git submodule update --remote operation does just that:

  1. cd into the submodule;
  2. run git fetch;
  3. use the branch name you set earlier (as the submodule's "branch" setting) to figure out a hash ID, as perhaps updated by the git fetch in step 2;
  4. run some second Git operation—such as git checkout—using that hash ID.

To do this manually—so that you have control over the result, rather than just assuming that some computer program has produced the right answer (i.e., 42) even if you're not sure what the question was—you would do these same steps, but probably actually look at what each one did before going on to the next one.

like image 187
torek Avatar answered Apr 23 '26 02:04

torek