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.)
R2
like normalgit submodule add <path to Q> S2
cd S2
S1
in R1
as remote git remote add S1 <path to R1>/S1
git fetch S1
git checkout -b localpatches S1/localpatches
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.