如何将一个子目录从一个git存储库中的分支移动到另一个存储库中的分支,并保留历史记录? [英] How to move a subdirectory from a branch in one git repository to a branch in a different repository, preserving history?

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

问题描述

我有一个包含实用程序库的目录,这些实用程序库是在一个git存储库中的一个分支中开发的,但事实证明它们确实属于另一个项目中的另一个目录.我已经阅读并尝试了Greg Bayer的将文件从一个Git存储库移动到另一个,多次保存历史记录,但是我无法保存历史记录.我正在尝试在非master分支下进行所有操作,以提高安全性,因为无论如何该项目还没有准备好合并回master.

I've got a directory containing utility libraries that were developed in a branch in one git repository, but it turns out they really belong in a different directory in a different project. I've read through and attempted Greg Bayer's Moving Files from one Git Repository to Another, Preserving History multiple times, but I'm unable to preserve history. I'm attempting to do this all under non-master branches to be more safe as the project is not ready to merge back to master yet anyway.

这是我到目前为止正在做的事情:

Here's what I'm doing so far:

准备将"DirectoryName"目录从"Repo1"存储库的分支"SomeBranch"中移出:

cd ~/Desktop
git clone git@github.com:username/Repo1.git
cd Repo1
git checkout -b SomeBranch origin/SomeBranch
git remote rm origin
git filter-branch --subdirectory-filter DirectoryName
mkdir DirectoryName
git mv *.php *.txt DirectoryName
git add DirectoryName
git commit -m "Stripped everything down to just DirectoryName."

将"DirectoryName"目录合并到"Repo2"存储库的"SomeBranch"分支中:

cd ~/Desktop
git clone git@github.com:username/Repo2.git
cd Repo2
git checkout -b SomeBranch origin/SomeBranch
git remote rm origin
git remote add Repo1 ../Repo1/
git pull Repo1 SomeBranch
git remote rm Repo1

执行此操作后,我可以成功地将所有内容剥离到Repo1中的"DirectoryName"(也可以将其拖到Repo2中),但是历史记录丢失了.如果我执行 git日志-DirectoryName git log-DirectoryName/SomeFile.php ,我只会看到将所有内容简化为DirectoryName".犯罪).因此,显然我的 git filter-branch 命令出了点问题,但是我对它不甚了解,无法弄清楚什么.

When I do this I can successfully strip everything down to "DirectoryName" in Repo1 (and I can pull it over to Repo2 as well), but the history is lost. If I do a git log -- DirectoryName or git log -- DirectoryName/SomeFile.php, I only see the "Stripped everything down to just DirectoryName." commit). So, clearly something is wrong with my git filter-branch command, but I'm not familiar enough with it to figure out what.

由于我们正在对代码库进行一些根本性的更改,因此任何建议都将不胜感激,因此随着内容的移动,我将需要相对频繁地执行此操作一段时间(但我们希望保留历史记录).

Any suggestions would be greatly appreciated as we're undergoing some fundamental changes to our codebase, so I'll need to be doing this relatively frequently for a while as stuff moves around (but we want to preserve the history).

更新:正如我在Repo1或中提到的 git log-DirectoryName (或 git log-DirectoryName/SomeFile.php )一样Repo2)除了将所有内容都精简到DirectoryName"之外,没有显示其他任何提交.提交,但是如果执行 git log ,我会看到正确的提交历史记录.我只是错误地使用了 git log 还是有一些损坏导致提交无法正确显示?

Update: As I mentioned git log -- DirectoryName (or git log -- DirectoryName/SomeFile.php; either in Repo1 or Repo2) does not show any commits other that the "Stripped everything down to just DirectoryName." commit, but if I do git log I see the correct commit history. Am I just using git log incorrectly or is there some corruption that's causing the commits to not show up correctly?

另一个更新: git log-DirectoryName 确实在我的原始未修改的Repo1中显示正确的提交,但是它确实正确不是在 git filter-branch 之后显示正确的提交(我已经尝试过 git filter-branch --subdirectory-filter DirectoryName---all 但也带有"master"分支,似乎没有必要...相同的结果).也就是说,提交历史记录是在运行 git filter-branch 之后存在的,我可以在 git log master中看到全部内容.它似乎不再与目录有关或文件.有什么想法吗?

Another Update: git log -- DirectoryName does show the correct commits in my original, unmodified Repo1, but it does not show the correct commits after the git filter-branch (and I've tried git filter-branch --subdirectory-filter DirectoryName -- --all but that mucks with the "master" branch as well and doesn't appear to be necessary... same result). That said, the commit history is there after running git filter-branch, I can see it all with git log master.. it just no longer seems to pertain to the directory or the files. Any ideas?

推荐答案

听起来您所做的一切都很好,只是对 git log 的误解而引起的问题.

It sounds as if what you've done is fine, it's just a misunderstanding about git log that's causing problems.

git仅在每次提交时存储树的状态,而不是记录将树从一次提交中的状态转移到下一次提交的更改.但是,如果您使用 git log 查找特定文件或目录的历史记录,则可以告诉它在文件历史记录用尽时尝试查找重命名.您可以执行以下操作:

git just stores the state of the tree at each commit, rather than recording the changes that took the tree from the state in one commit to the next. If you're using git log to find the history of a particular file or directory, however, you can tell it to try to look for renames when the history of the file appears to run out. You can do that with:

git log --follow -- DirectoryName

或者,如果这行不通,请尝试仅对单个文件进行尝试,例如

Or if that doesn't work, try it for just a single file, e.g.

git log --follow -- DirectoryName/whatever.txt

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

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