简体   繁体   中英

How to copy a submodule from one repo to another?

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.)

As .netime 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM