git在保留历史记录的同时将目录移动到另一个存储库 [英] git move directory to another repository while keeping the history

查看:299
本文介绍了git在保留历史记录的同时将目录移动到另一个存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先-很抱歉提出这个问题.已经有很多关于它的话题.但是我对他们没什么运气.我对git的不熟悉并没有太大帮助.

First - apologies for asking this question. There are a lot of topics about it already. But I'm not having much luck with them. My lack of familiarity with git is not much help.

我要将一个文件夹从一个git repo移到另一个(已经存在).例如

I'm moving a folder from one git repo to another (which already exists). e.g.

repo-1
---- dir1
---- dir2
---- dir3
---- dir-to-move
---- dir5

repo-2
---- dir1
---- dir2
---- dir3

最后,我希望存储库看起来像

In the end I want the repos to look like

repo-1
---- dir1
---- dir2
---- dir3
---- dir-to-move
---- dir5

repo-2
---- dir1
---- dir2
---- dir3
---- dir-to-move

即一段时间内,两个仓库中都将存在移动目录.但是最终,我将最新的更改迁移到 repo-2 并从 repo-1 中删除要移动的.

i.e. For a time dir-to-move will exist in both repos. But eventually I'll migrate the latest changes to repo-2 and remove dir-to-move from repo-1.

我的最初研究使我相信我需要使用 filter-branch .例如

My initial research made me believe that I needed to use filter-branch. e.g.

如何使用git format-patch和git am将文件从一个git存储库移动到另一保存历史记录

此后,我了解到 subtree 取代了该方法.但是,它没有达到我的预期.我以为我可以做类似的事情

I've since learnt that subtree superseded that approach. However it's not doing what I expected. I thought I'd be able to do something like

repo-1 工作区中

git subtree split -P dir-to-move -b split

split 分支过滤到仅 dir-to-move 及其历史记录. 然后在 repo-2 工作区

to filter the split branch down to only dir-to-move and it's history. Then in repo-2 workspace

git remote add repo-1 repo-1-url.git
git subtree add --prefix dir-to-move split

这确实会移动代码.它还包括历史记录

This does move the code across. It also, sort of, includes the history

例如

cd repo-2
git log

显示来自 repo-1

但是

cd repo-2
git log dir-to-move

仅显示从提交中添加要移动的目录...."

Shows only an 'Add dir-to-move from commit ....'

即历史记录已包括在内,但在检查特定文件/目录时不会显示.

i.e. The history is included but does not show up when checked for the specific files/directories.

如何正确执行此操作?

推荐答案

git subtree我帮不上忙,但是filter-branch可以帮上忙.

I can't help you with git subtree, but with filter-branch it's possible.

首先,您需要创建一个公共存储库,其中将包含源分支和目标分支.这可以通过在来源"旁边添加新的远程"并从新的远程获取来完成.

First you need to create a common repository that will contain both source and destination branches. This can be done by adding a new "remote" beside "origin" and fetching from the new remote.

在源分支上使用filter-branchrm -rfdir-to-move以外的所有目录.之后,您将拥有一个提交历史记录,该记录可以被完全地重新建立基础或合并到目标分支中.我认为最简单的方法是从源分支中cherry-pick所有非空提交.这些提交的列表可以通过运行git rev-list --reverse source-branch -- dir-to-move

Use filter-branch on the source branch to rm -rf all directories except dir-to-move. After that you'll have a commit history that can be cleanly rebased or merged into the destination branch. I think the easiest way is to cherry-pick all non-empty commits from the source branch. The list of these commits can be obtained by running git rev-list --reverse source-branch -- dir-to-move

当然,如果dir-to-move的历史记录是非线性的(已经包含合并提交),那么它不会被Cherry-pick保留,因此可以使用git merge代替.

Of course, if the history of dir-to-move is non-linear (already contains merge commits), then it won't be preserved by cherry-pick, so git merge can be used instead.

创建公共仓库的示例:

cd repo-2
git remote add source ../repo-1
git fetch source

示例过滤器分支

cd repo-2
git checkout -b source-master source/master
CMD="rm -rf dir1 dir2 dir3 dir5"
git filter-branch --tree-filter "$CMD"

示例性地摘录到目标主机

Example cherry-pick into destination master

cd repo-2
git checkout master
git cherry-pick `git rev-list --reverse source-master -- dir-to-move`

这篇关于git在保留历史记录的同时将目录移动到另一个存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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