I have a git repository that I have done some testing on and I'd like to include it in the main repository.
I would like to add this as an orphaned branch, because I want to keep it but I don't want to bloat the master branch with my test project.
How can I import the git repository as an orphaned brach? I'd like to keep my commits.
Edit to show it's not a duplicate: I didn't actually care about it being orphaned or anything like that, I just wanted to have a way to merge 2 repositories. The given answer is correct and solves my problem.
I think you're looking at this wrong. In fact, the existence of this question implies something that is not true. I think what you want is more correctly named "disjoint subgraphs".
To get there, bear with me through this next exercise. If you just want an answer, scroll down. :-)
Exercise: which branch is orphaned?
Imagine you have some repository, and in this repository there are two branches named
XandY(nomaster, justXandY). BranchXpoints to commita123456and branchYpoints to commitb987654. These commits point to their parents as usual, but if we draw the entire commit graph (the repository is small so this fits nicely in this SO answer) we get:There are two different root commits. Branch
Xpoints toa123456which is the tip of a four-commit chain rooted ata000000. BranchYpoints tob987654which is the tip merge commit of a six-commit structure rooted atb000000.Which branch(es), if any, is an orphan branch?
(This is a good place to stop and think about that question.)
If you chose
X, why did you choose that one? If you choseY, why did you choose that one? If you chose both, I think you could justify that choice; but if you answered neither, I would call that correct and note that you have now agreed with Git.(If you chose both
XandY, what is your reason for calling these "orphan branches"? I think you could argue for this as a correct answer, and justify it by saying that neither one is connected tomaster. If we then added a branch namedmasterand made it point to any of the four commits onX, you would then say that branchYis orphaned andXis not. If you mademasterpoint to any of the six commits onY, you would then say thatXis the orphan andYis no longer orphaned. There are probably more ways to decide, depending on how twisty you want to get with how you define "orphan". But that's not what Git means, and in fact, there is better terminology, which will not disagree with Git, and will serve you better in the long run.)Disjoint subgraphs
In our exercise example repository above, neither
XnorYwas an orphan branch, because both have commits on them. In graph theory—Git repositories are built around graph theory—the two branch namesXandYpoint to disjoint subgraphs of the overall graph.Side note: what Git means by orphan branch
In Git, an orphan branch, created with
git checkout --orphan newbranch, is a branch in special state, "yet to be born". This state lasts until there are commits on the branch, after which the branch is no longer an "orphan branch". Now it's just an ordinary branch, like any other.Git actually implements the orphan-branch state in a way that causes the abstraction to leak rather badly. Specifically, it writes the new branch name into
HEAD, but nowhere else. SinceHEADcontains the name of the current branch, if you check out any other branch, this overwrites theHEADfile, and the orphan branch itself vanishes completely. In other words, only one single branch can ever be in "orphan" state, and it gets there by having its name stored inHEADand only inHEAD. SinceHEADstores the name of the current branch, only the current branch can be an orphan.At last, the answer to what I think your question really is
Given two unrelated repositories R1 and R2, you want to make a union repository (possibly a new repository R3, perhaps just modifying one of the two original repositories) whose commit graph is a disjoint graph resulting from the union the two commit graphs in R1 and R2.
Let's make this as a new third repository. (It's even easier to just glom R1 into R2 or vice versa, by omitting some of these steps, so let's start with the full exercise and you can scale it back if you want.)
To build the new repository R3, first, create an empty repository:
Now fetch everything from R1 into R3. You can do this by adding R1 as a named remote (this is almost certainly the way to go if you are adding R2 into R1, though then we're now in R1 adding R2 rather than in R3 adding R1). You could just run
git fetchand give it a URL and use the branch information that it drops intoFETCH_HEAD, but let's go with adding both as remotes. The<url>s here can be paths (../../otherrepo.gitor/path/to/otherrepo.git), orssh://...orgit://...orhttps://..., as usual.Then, fetch everything from R2 into R3. We use exactly the same steps, just a different remote name and URL:
At this point you are essentially done: you have the union as the commit graph in your new repository. The only thing left to do is to make local branch names pointing to the same commits as some or all of the remote-tracking branches.
Since we named repository R1
r1, all of its branches are inrefs/remotes/r1/branch-name. Likewise, since we named R2r2, all of its branches are inrefs/remotes/r2/branch-name. You can refer to these by their shortened versions,r1/whateverandr2/whatever. Let's sayr1has branchesmasterandAandr2has branchesmasterandB.Because there is only one
A, you can do:and you now have a branch named
Apointing to the same commit asr1/A. Moreover, your localAis set to trackr1/A: that is, its upstream isr1/A.The same holds for
B.You can create a new
masterinr3but it can only point to the same commit as one of the other two repositories, and you cannot use this shortcut. Let's say you wantr3'smasterto matchr2/master:will create it and set it up to track
r2/master.If you don't want this upstream
--tracksetting, create the branches using--no-track.That is all there is to it: creating these two disjoint subgraphs really is that easy. You just need to know that this is what you are doing, and that fussing about with
--orphanis only required if you want to grow a new disjoint subgraph organically, rather than fetching it from some existing repository.