简体   繁体   English

在重写路径时将本地git存储库迁移到更大的远程存储库

[英]Migrating local git repository into larger remote repository while rewriting paths

I have been happily using git on my machine for a project "MyProject". 我一直很乐意在我的机器上使用git作为项目“MyProject”。 MyProject is actually part of a larger collaborative research effort "WholeThing". MyProject实际上是更大的合作研究工作“WholeThing”的一部分。 There is virtually no overlap between myProject and OtherProjects in the WholeThing. 在WholeThing中,myProject和OtherProjects之间几乎没有重叠。

Now, Big Bosses have decided to put all development onto a central git repository (for documentation and backup). 现在,Big Bosses决定将所有开发都放在一个中央git存储库(用于文档和备份)。 Makes sense to me so far. 到目前为止对我有意义。

Assume I have no privileged access to the central repository. 假设我没有访问中央存储库的特权。

The problem is, that MyProject maps into a sub-directory of the WholeThing, ie 问题是,MyProject映射到WholeThing的子目录,即

local repo:
MyProject -|
           |- .git
           |
           |- dirs


remote repo:
WholeThing -|
            |- .git
            |
            |- Area1
            |
            |- Area2
                 |------ MyProject
                 |------ DudesProject

How do I get my local repository into the proper sub-directory on the central repository while preserving history and branches? 如何在保留历史记录和分支的同时将本地存储库放入中央存储库中的正确子目录中?

I (think I ;) understand remote branches and stuff, but I have not yet figured out a way to prefix the path of MyProject's file with "WholeThing/Area2" short of moving the files in my local repository and creating a commit before pushing. 我(想想我;)了解远程分支和东西,但我还没有找到一种方法来为MyProject文件的路径添加前缀“WholeThing / Area2”,而不是移动本地存储库中的文件并在推送之前创建提交。 There surely must another way? 肯定还有另一种方式吗?

I also do not mind to clone the WholeThing and navigate down to MyProject once all the migration is done ... so I am not looking for submodules or such (and I don't have administrative access to the central repository). 我也不介意克隆WholeThing并在完成所有迁移后导航到MyProject ...所以我不是在寻找子模块等(并且我没有对中央存储库的管理访问权限)。

Any ideas are welcome! 欢迎任何想法!

Cheers, Jose 干杯,何塞

Thanks for the replies so far ... made me formulate the question differently on Google. 感谢您到目前为止的回复...让我在Google上以不同的方式提出问题。

The answer is actually written down in the man page, you just need to read to the very end. 答案实际上写在手册页中,你只需要阅读到最后。

The solution is to rewrite the history with git filter-branch and rewriting the path of each file while doing so. 解决方案是使用git filter-branch重写历史记录,并在执行此操作时重写每个文件的路径。 Rewriting the path can in principle be done with --tree-filter (I think), or with --index-filter , which is much faster. 重写路径原则上可以使用--tree-filter (我认为),或使用--index-filter ,这要快得多。

The code snippet below is take allmost verbatim from the man page git help filter-branch , just edited to reflect the directories in my example. 下面的代码片段是从手册页git help filter-branch逐字逐句,只是编辑以反映我的示例中的目录。 It asks for each commit for the involved file's pathnames, rewrites them prefixing "WholeThing/Area2/MyProject/" and replays the commits. 它要求每个提交所涉及文件的路径名,重写它们前缀为“WholeThing / Area2 / MyProject /”并重放提交。

git filter-branch --index-filter \
    'git ls-files -s | sed "s-\t-&WholeThing/Area2/MyProject/-" |
     GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
     git update-index --index-info &&
     mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

The next (hopefully simple) step is to add the remote repository and push to it. 下一个(希望很简单)步骤是添加远程存储库并推送到它。

Cheers & thanks! 干杯谢谢!

yes the next step is nice and simple 是的,下一步很简单

git remote add other [path/to/repo]
git fetch other
git checkout -b other-master other/master
git checkout master
git merge other-master

and you should be good to go 你应该好好去

Ideally you should your own sub project MyProject.git under Wholething/Area2. 理想情况下,您应该在Wholething / Area2下拥有自己的子项目MyProject.git。

If your bosses want to have only one git repository for every project, that doesn't make sense, but if they want it anyway, that would mean the WholeThing tree is a new tree totaly different from your and so , you can't do anything except from pulling the whole thing, add your directory and push everything (as well as the DudesProject) (you can NOT work on a subset or subdirectory of a git tree). 如果你的老板想要为每个项目只有一个git存储库,这没有意义,但是如果他们仍然想要它,那就意味着WholeThing树是一个完全不同于你的新树,你不能这样做除了拉动整个东西,添加你的目录并推送所有东西(以及DudesProject)(你不能在git树的子集或子目录上工作)。 Anyway as that would be 2 different trees you will lose all your history and stuff. 无论如何,那将是2棵不同的树,你将失去所有的历史和东西。

The proper way to do stuff is to have one git tree per project and so, so no .git under WholeThing. 做事的正确方法是每个项目都有一个git树,所以在WholeThing下没有.git。 With the proper way "WholeThing/Area2" appears just naturaly in the url when you are adding the remote branch. 使用正确的方法,当您添加远程分支时,“WholeThing / Area2”在URL中自然出现。

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

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