提取 git 存储库的一部分?

发布于 2024-08-26 23:06:59 字数 274 浏览 6 评论 0原文

假设我的 git 存储库具有以下结构:

/.git
/Project
/Project/SubProject-0
/Project/SubProject-1
/Project/SubProject-2

并且该存储库有相当多的提交。现在,其中一个子项目 (SubProject-0) 变得相当大,我想将 SubProject-0 取出并将其设置为一个独立项目。是否可以从父 git 存储库中提取涉及 SubProject-0 的所有提交历史记录并将其移动到新的存储库?

Assume my git repository has the following structure:

/.git
/Project
/Project/SubProject-0
/Project/SubProject-1
/Project/SubProject-2

and the repository has quite some commits. Now one of the subprojects (SubProject-0) grows pretty big, and I want to take SubProject-0 out and set it up as a standalone project. Is it possible to extract all the commit history involving SubProject-0 from the parent git repository and move it to a new one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-09-02 23:06:59

请参阅http://git-scm.com/docs/git-filter-branch

我认为你需要类似

git filter-branch --subdirectory-filter Project/SubProject-0 --prune-empty -- --all

存储库克隆的东西。

See http://git-scm.com/docs/git-filter-branch

I think you need something like

git filter-branch --subdirectory-filter Project/SubProject-0 --prune-empty -- --all

in a clone of the repository.

骄傲 2024-09-02 23:06:59

我需要做类似的事情,但我想本质上将子项目从一个存储库移动到另一个存储库。我所做的是使用 fetch,因为它可以从任何来源获取对象。

因此,基本上,我创建了一个新分支,删除了该分支中不需要的内容,然后使用 git fetch 将分支从一个存储库拉到另一个存储库。一旦我有了对象,合并就成功了。

例如,

在包含原始内容的存储库上:

    git checkout -b temp master
    git rm -r Unneeded_stuff
    git commit -m 'pruning'

然后您可以将该分支从一个存储库获取到另一个完全不同的存储库(不相关):

    cd /path/to/other/repository
    git fetch /path/to/source/repo temp:temp

其中 temp:temp 表示“在源上获取临时并将其保存为此处的临时”。从那里您可以将结果合并到您的母版中。

    git merge temp

然后,您可以删除临时分支,因为在第一种情况下,您不想将其与原始存储库合并,而在第二种情况下,您已将其合并。

我确信这些步骤可以稍微压缩一下,但是这套看起来很好而且很清晰。

I needed to do something similar, but I wanted to essentially move a subproject from one repo to another. What I did instead was to use fetch, since it can fetch objects from any source.

So, basically, I created a new branch, deleted the unneeded stuff in that branch, then used git fetch to pull the branch from one repo to another. Once I had the objects, merge did the trick.

E.g.

On the repository that has the original stuff:

    git checkout -b temp master
    git rm -r Unneeded_stuff
    git commit -m 'pruning'

Then you can fetch that branch from one repository into a completely different one (unrelated):

    cd /path/to/other/repository
    git fetch /path/to/source/repo temp:temp

where temp:temp means "fetch temp on the source and save it as temp in here". From there you can merge the result into your master.

    git merge temp

You can then delete the temp branches, since in the first case it isn't something you ever want to merge with the original repo, and in the second case you have merged it.

I'm sure these steps could be compressed a bit, but this set seems nice and clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文