0

I have a git repo R1 with a submodule S1, and the latter has two branches, master and localpatches.

Let Q represent the source repo of the S1 submodule (in other words, Q is the argument that was given to the git submodule add ... that initially created the S1 submodule in R1).

For the purpose of this question you may assume that S1's localpatches branch is a direct descendant of its master branch.

Furthermore, you may ssume that all the commits in S1's master branch come from Q, while the commits in the range master..localpatches are present only in S1.

Now, I also have a separate git repo R2, that is completely distinct from R1. (By this I mean that R1 and R2 have no commits in common, they have different contributors, their associated work trees are completely different, etc.)


I would like to know the cleanest way to add a submodule S2 to R2 such that it is a replica of the S1 submodule of R1. In particular, (1) S2's source repo should also be Q; (2) the S2 should have two branches, master and localpatches; (3) the commits in the master (resp.localpatches) branches of S1 and S2 should be identical.

(Please correct me if I'm wrong, but the first of these requirements (namely, that S2's source repo should be Q) argues against creating S2 by just running git submodule add <S1's URL> in R2.)

(In case it matters, you may assume that the R1 repo is obsolescent/defunct. This means, in particular, that the issue of the future divergence between S1 and S2 is moot.)

2
  • Why not push you localpatches branch to Q, maybe rename it. And then add your submodule from there? Commented Oct 24, 2020 at 17:06
  • @ian: Q is owned by someone else, and they tend take months to consider, let alone accept, pull requests... Commented Oct 24, 2020 at 17:36

1 Answer 1

1

As onetime operation

  1. create repo R2 like normal
  2. add submodule from Q git submodule add <path to Q> S2
  3. when you cd into a submodule you can modify it cd S2
  4. add S1 in R1 as remote git remote add S1 <path to R1>/S1
  5. fetch git fetch S1
  6. create a branch git checkout -b localpatches S1/localpatches
  7. maybe remove S1 remote git remote remove S1

Now S2 has the same commits as S1. I showed this solution because you mentioned the link to R1/S1 is not important to you.


If you want to do this multiple times or want to share code between S1 and S2. It would be better to create a fork of Q. (could even be local) and add your fork as a remote to S1 and S2.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.