如何将文件从一个git repo移动到另一个(不是克隆),保存历史记录 [英] How to move files from one git repo to another (not a clone), preserving history
问题描述
我们的Git仓库最初是作为单个SVN仓库的一部分,其中每个项目都有自己的树,如下所示:
project1 / branches
/ tags
/ trunk
project2 / branches
/ tags
/ trunk
显然,使用 svn mv
将文件从一个文件移动到另一个文件相当容易。但在Git中,每个项目都在它自己的仓库中,今天我被要求将一个子目录从 project2
移动到 project1
。我做了这样的事情:
$ git clone project2
$ cd project2
$ git filter-branch --subdirectory-深度过滤/埋藏/ java / source /目录/ A - --all
$ git remote rm origin#所以我不会无意中回购;-)
$ mkdir -p深入/埋藏/不同/ java / source /目录/ B
$ for f in * .java;做
> git mv $ f deeply / buried / different / java / source / directory / B
>完成
$ git commit -m将文件移动到新的子目录
$ cd ..
$
$ git clone project1
$ cd project1
$ git remote add p2 ../project2
$ git fetch p2
$ git branch p2 remotes / p2 / master
$ git merge p2# - 允许git使用的无关历史记录2.9
$ git remote rm p2
$ git push
但这似乎相当复杂。总体来说有没有更好的方法来做这种事情?或者我是否采用了正确的方法?
/ code>过滤分支
是关键。事实上,你使用它基本上证明有没有更简单的方法 - 你别无选择,只能重写历史,因为你想最终只有一个(重命名)的文件的子集,这通过定义改变散列。由于没有任何标准命令(例如 pull
)重写历史记录,因此您无法使用它们来完成此操作。 <当然,你可以细化细节 - 你的一些克隆和分支并不是绝对必要的 - 但总体方法很好!这太遗憾了,这很复杂,但当然,git的重点不在于轻易重写历史记录。
Our Git repositories started out as parts of a single monster SVN repository where the individual projects each had their own tree like so:
project1/branches
/tags
/trunk
project2/branches
/tags
/trunk
Obviously, it was pretty easy to move files from one to another with svn mv
. But in Git, each project is in its own repository, and today I was asked to move a subdirectory from project2
to project1
. I did something like this:
$ git clone project2
$ cd project2
$ git filter-branch --subdirectory-filter deeply/buried/java/source/directory/A -- --all
$ git remote rm origin # so I don't accidentally the repo ;-)
$ mkdir -p deeply/buried/different/java/source/directory/B
$ for f in *.java; do
> git mv $f deeply/buried/different/java/source/directory/B
> done
$ git commit -m "moved files to new subdirectory"
$ cd ..
$
$ git clone project1
$ cd project1
$ git remote add p2 ../project2
$ git fetch p2
$ git branch p2 remotes/p2/master
$ git merge p2 # --allow-unrelated-histories for git 2.9
$ git remote rm p2
$ git push
But that seems pretty convoluted. Is there a better way to do this sort of thing in general? Or have I adopted the right approach?
Yep, hitting on the --subdirectory-filter
of filter-branch
was key. The fact that you used it essentially proves there's no easier way - you had no choice but to rewrite history, since you wanted to end up with only a (renamed) subset of the files, and this by definition changes the hashes. Since none of the standard commands (e.g. pull
) rewrite history, there's no way you could use them to accomplish this.
You could refine the details, of course - some of your cloning and branching wasn't strictly necessary - but the overall approach is good! It's a shame it's complicated, but of course, the point of git isn't to make it easy to rewrite history.
这篇关于如何将文件从一个git repo移动到另一个(不是克隆),保存历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!