简体   繁体   中英

Move Existing git Repository into One Repository with Orphan Branch

I was reading online about git and the suggestion is to have a git repository per project. Currently my setup follows that rule, but recently the number of projects I have is increasing (= number of repositories are increasing) and I would like to move common projects into one repositories each under orphan branch (kind of categories them).

For example, if I have P1 and P2 repositories which have histories, what I would like is to create a repository P with 2 orphan branches P1 and P2 and transfer history from P1 and P2 repositories to the corresponding branches in P repository.

I was wondering if someone can show me (git command) on how to transfer history P1 and P2 to P orphan branch.

Thank you,

To accomplish what you asked you can do in repository P

git remote add P1 urlToP1
git remote add P2 urlToP2
git fetch --all

You will find all the branches of P1 and P2 as P1/branchName . You can then create local branches.

The key point this is in the git fetch doc

Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories .

Those objects include commits. Since the two original repositories P1 and P2 do not share any commit, you end up with two separate graphs.

Great suggestion Francesco . While doing git fetch works, I would constraint it a bit by specifying the source:target branches, and using your local P1 and P2 repositories. I'd also like to complement your answer in order to fully cover the original question.

The solution

Let's say you want to move the master branches of P1 and P2 into two new non existent branches on P , named p1 and p2 , respectively (do not create the p1 and p2 branches in advance).

  1. Add the remote repositories on P git remote add P1 ~/path/to/P1 git remote add P2 ~/path/to/P2

    You could also specify an url to the repo instead of the path.
    ie git remote add P1 urlToP1

  2. Fetch only what you need! (the n flag excludes tags, you may want to take it out) git fetch -n P1 master:p1 git fetch -n P2 master:p2

    syntax: git fetch -n <remote> <sourceBranchOnP1>:<targetBranchOnP>.

  3. Remove the remote repositories git remote remove P1 git remote remove P2

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