问题
I've got a single git repo with multiple orphan branches containing their own separate and unrelated history. Structure is similar to
C:\repos\original-git-repo>git branch
orphan1
*orphan2
orphan3
This has turned out to be a poor structure for my workflow.
How do I easily extract each of these orphan branches into standalone repos while (a) maintaining each orphan's branch history, and (b) each new standalone repo contains only the objects from it's matching orphan branch, not all objects from the original repo?
Resulting dir tree of git repos would look like
C:\repos>
|- original-git-repo/
|- orphan1/
|- orphan2/
`- orphan3/
UPDATE: as mentioned below, a normal clone of the original repo appears to clone all objects into the new repo's .git/objects dir rather than just the objects for a specific orphan branch. The best I've been able to do for minimal new repo size is this overly manual recipe for each original orphan branches.
$ git init [--bare] <new repo>
# push orphan branch from orig to new repo as `push.default = current` in config
$ git remote add <new repo name> <path>
$ git push <new repo name>
# manually copy relevant tags from orig repo to new repo's `.git/refs/tags` dir
Cloning using --local --no-hardlinks --branch orphan1 --single-branch also appears to clone all orig repo objects into new repo even though only one local/remote branch pair is cloned.
UPDATE2 - PREFERRED SOLUTION: As both answers below show, one can solve this in multiple ways via git init, or git clone and some manual editing of .git/packed-refs. I'm using this recipe based upon the accepted answer from below.
git init orphan1
cd orphan1
git fetch C:/repos/original-git-repo orphan1:orphan1
git checkout orphan1
git branch -m master
回答1:
You can fetch anything you want with just a url and refname
git init orphan1
cd orphan1
git fetch /path/to/original orphan1:orphan1
and likewise with the others.
回答2:
It's pretty easy. Basically, for each branch, you just have to create a copy of the repo, set the desired branch as "master", delete the others, and remove loose objects:
$ git clone <original repo> <new repo>
$ git remote rm origin # Optionally remove reference to original repo
$ git checkout <desired branch>
$ git branch -m master
# Delete all unwanted branches
# Optionally, remove all unreachable objects:
$ git prune --expire=now
$ git gc --aggressive
来源:https://stackoverflow.com/questions/27493917/extracting-git-orphan-branches-into-standalone-repos