从已经存在的repo的子目录创建新的git repo [英] Create new git repo from already existing repo's subdirectory

查看:398
本文介绍了从已经存在的repo的子目录创建新的git repo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个已经存在的repo的子文件夹中创建一个单独的repo。



将子目录分离(移动)到单独的Git存储库中显示了这一点。但是,我无法得到一个干净的回购。我最终遇到了两个新的回购问题:


  1. 历史似乎重复;

以下是我所做的:

pre> $ git clone ssh://.../repo.git
$ cd repo
$ git filter-branch --subdirectory-filter subdirectory HEAD - - all
$ git reset --hard
$ git gc --aggressive
$ git prune

之后,看起来我有回购的历史和新的历史。我输入git log - all --graph(或gitk --all)我看到,作为第一次提交,第一次回购的初始提交。然后该图显示直到最后一次提交为止的原始回购的完整历史记录。然后,我在第一个历史记录的第一个历史记录上仅显示了我想要的子文件夹的历史记录。但在那部分历史中,我只有主,没有分支/合并。

git log,gitk或gitg都只显示扁平历史记录:他们没有在子文件夹的扁平化历史记录之前显示最初的回购记录。



我试着只使用filter-branch命令,克隆生成的回购使用:

  $ git filter-branch --subdirectory-filter子目录 -  --all 

而不是:

  $ git filter-branch --subdirectory-filter subdirectory HEAD  -  --all 

但结果相同。



我做错了什么或者git坏了?我真的没有想法...使用git 1.7.6。编辑:我认为这个问题可能来自合并提交被filter-branch忽略的事实,因此给出了没有分支或合并的平坦历史。 。

解决方案

这里有两个问题:

正如Kevin Ballard指出的那样,您需要删除.git目录中的refs / original目录以摆脱伪造的日志条目; IIRC在您提及的问题中提到了这一点。

(2)您必须一次转换一个分支。据我所知,这在任何地方都没有提及,但很容易从经验中发现。这样做的脚本看起来就像这样:

$ $ $ $ $ $ $ $ $ $($ git for-each-ref --format = '%(refname:short)'refs / remotes / origin | grep -v HEAD); do
git checkout -b $(basename $ branch)$ branch
git filter-branch -f --subdirectory-filter subdirectory HEAD - --all
done
code>

请注意,您需要-f或其他类似于--original $(basename $ branch)-original来强制git重用或者重命名它存储原始参考的命名空间。另外请注意,如果子目录在特定分支上不存在,您可能会看到找不到任何重写内容的消息 - 您可能希望从新的存储库中删除这些分支。 (或者在运行脚本之前删除它们。)


I want to create a separate repo from an already existing repo's subfolder.

Detach (move) subdirectory into separate Git repository shows exactly that. BUT, I can't get a clean repo with it. I end up with two problems with the new repo:

  1. The history seems duplicated;
  2. I can't keep the branches history.

Here is what I did:

$ git clone ssh://.../repo.git
$ cd repo
$ git filter-branch --subdirectory-filter subdirectory HEAD -- --all
$ git reset --hard
$ git gc --aggressive
$ git prune

After this, it seems I have the original history of the repo AND the new history. I type "git log --all --graph" (or gitk --all) I see, as the first commit, the initial commit of the first repo. The graph then shows the original repo's full history until the last commit. Then, I have a SECOND history on top of the first one showing the history of only the subfolder I want. But in that part of the history I only have "master" and no branches/merges.

"git log", gitk or gitg all show the "flatten" history only: they don't show the initial repo's history before the the subfolder's flatten history.

I tried using just the "filter-branch" command, cloning the resulting repo (with --no-hardlinks), using:

$ git filter-branch --subdirectory-filter subdirectory -- --all

instead of:

$ git filter-branch --subdirectory-filter subdirectory HEAD -- --all

But with the same result.

Am I am doing something wrong or git is broken? I'm really out of ideas... Using git 1.7.6. Thanks.

EDIT: I think the problem might come from the fact that merge commits are ignored by filter-branch, thus giving a flat history with no branches or merges...

解决方案

You have two problems here:

(1) As Kevin Ballard points out, you need to delete the refs/original directory in your .git directory to get rid of the spurious log entries; IIRC this is mentioned in the question you referred to.

(2) You have to convert the branches one at a time. As far as I know, this isn't mentioned anywhere, but it's pretty easy to discover empirically. A script to do this would look something like this:

for branch in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin | grep -v HEAD); do
  git checkout -b $(basename $branch) $branch
  git filter-branch -f --subdirectory-filter subdirectory HEAD -- --all
done

Note that you need the -f or else something like --original $(basename $branch)-original to force git to reuse or rename the namespace where it stores the original refs. Also note that you may see messages like "Found nothing to rewrite" if subdirectory does not exist on a particular branch -- you will probably want to delete those branches from your new repository. (Or delete them before running the script.)

这篇关于从已经存在的repo的子目录创建新的git repo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆