简体   繁体   English

使用不同的目录结构在git存储库中移动补丁

[英]Moving patches in git repository with different directory structure

I'm sorry for the somewhat verbose and confused title. 抱歉,标题有些冗长和混乱。 We have a git repository with the following directory structure (the root represents the repository root; the names are fictional): 我们有一个具有以下目录结构的git存储库(根代表存储库根;名称是虚构的):

/
/dir1/
/dir2/
/dir2/subdir1/
/dir2/subdir2/

A team colleague created a new git repository with a copy of /dir2/subdir1 , ie the /dir2/subdir1 became the root of the new repository. 一个团队同事用/dir2/subdir1的副本创建了一个新的git存储库,即/dir2/subdir1成为了新存储库的根。 We did some (~15) commits in that new repository, then we decided that we would like to replicate the new commits in the old repository. 我们在该新存储库中做了一些(〜15)次提交,然后我们决定要在旧存储库中复制新提交。

What is the best way to perform this action? 什么是执行此操作的最佳方法?

Of course we could create a patch from the new repository and apply it as single commit in the old one, but we would lose granularity of history by doing so. 当然,我们可以从新存储库中创建一个补丁并将其作为旧提交中的单个提交应用,但是这样做会丢失历史的详细程度。

There have been no commits in the old repository in the meantime that touched /dir2/subdir1 , so there should definitely be no conflicts. 在此期间,旧存储库中没有触及/dir2/subdir1提交,因此绝对不应有冲突。

The solution was to use git format-patch and git am . 解决方案是使用git format-patchgit am In the new repository I did a 在新的存储库中,我做了一个

git format-patch $revision

, where $revision is the revision in the new repository which corresponds to the status of the old repository, ie the revision where /dir2/subdir1 was added. ,其中$revision是新存储库中与旧存储库状态相对应的修订,即添加/dir2/subdir1的修订。 This created around 15 files containing the patches of every single commit. 这大约创建了15个文件,其中包含每个提交的补丁。

Then I moved to the old repository and used git am to apply the patches just created. 然后,我移至旧的存储库,并使用git am应用刚刚创建的补丁。 I just had to specify the directory where these patches had to be applied: 我只需要指定必须应用这些补丁的目录:

git am --directory dir2/subdir1 $patches

, where $patches are the files created during the previous step. ,其中$patches是在上一步中创建的文件。

Now the commits are moved from the new repo into the old one and I'm happy! 现在,提交已从新存储库移到旧存储库,我很高兴!

Option1 : Create patch of each commit separately ( git format-patch ), and apply in order to the old repo 选项1 :分别创建每个提交git format-patchgit format-patch ),然后按顺序应用于旧仓库

Option2 : Add the new repo as remote to old one, fetch the branch, Checkout the branch on your local repo and cherry-pick all commits (With gitk it is even easier`) 选项2 :将新存储库添加为旧存储库的远程存储库,获取分支,在本地存储库中检出该分支,并选择所有提交(使用gitk则更容易)。

Option3 :Add the new repo as remote to old one, fetch the branch, and merge it with the branch in old repo. 选项3 :将新存储库添加为旧存储库的远程存储库,获取分支,并将其与旧存储库中的分支合并。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM