git代码库迁移保留commit历史_git在保留历史记录的同时将目录移动到另一个存储库...

本文围绕将一个git仓库中的文件夹迁移到另一个已有仓库并保留commit历史展开。最初尝试用git subtree未达预期,后介绍用filter - branch的方法,包括创建包含源和目标分支的公共仓库、过滤源分支、挑选非空提交等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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

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.

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

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

In repo-1 workspace

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

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

e.g.

cd repo-2

git log

Shows commits from repo-1

but

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.

How can I do this properly?

解决方案

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.

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

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.

Example create common repo:

cd repo-2

git remote add source ../repo-1

git fetch source

Example filter branch

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`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值