将文件/文件夹从一个git仓库移到另一个仓库 [英] Move files / folders from one git repo to another

查看:1152
本文介绍了将文件/文件夹从一个git仓库移到另一个仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Github上有一个包含文件夹和文件的存储库,我刚创建了四个新的存储库。

到我刚刚创建的新存储库。



有没有一种方法可以使用GUI或CMD(Macbook)来做到这一点?



如果使用图形用户界面你想要提取的东西都包含在它们自己的文件夹中,在命令行中很容易。下面解释一个使用 git filter-branch --subdirectory-filter 的方法:

https://stackoverflow.com/a/12818479/955926



如果您的文件和文件夹不是很好包含,或不匹配一个简单的模式(即只保存* .py文件等),一种方法 - 我称之为梳理 - 就是继续运行简单的过滤器分支命令 - 在一个克隆,而不是原来的! - 继续剥离它的一部分,直到你的历史只包含你想要的东西。



当然,我们总是希望能够验证我们正在做的事情。这将显示一个有序的,唯一的列表,其中包含所有相关的文件/文件夹路径: log --pretty ='format:'--name-only | grep -v'^ $'| sort -u

假设您在该列表中看到 foo bar / baz 文件夹,您希望从历史记录中删除(即删除 baz ,它位于 bar foo ):

  $ git filter-branch -f --prune-empty --index-filter'git rm -rf --cached --ignore -unmatch foo bar / baz'
log
命令来验证那些链接是不存在的。 $ b> 随时随地,并从较小的列表中选择更多的东西来摆脱。或者你可以坚持每一个路径进入一个调用filter-branch。我更喜欢采取更小的步骤和验证,就像我更细心地提交细节一样。



过滤器分支每次都会进行备份,并且第二轮运行将因其存在而停止。这就是 -f 的用途。每当你再次过滤时,你都会覆盖你的备份,但是没关系,因为你是在克隆上做这个(right !?),如果你搞得很糟糕,你总是可以在新克隆上重做。



最后,您需要清除 refs / original / * 标题,这将指向在每次传球后你要过滤的分支的最后一个头部。如果你过滤分支了 master 分支,你会得到一个 refs / original / refs / heads / master 文件指向主的老头,预过滤。当你强制下一个通行证时,你会指向新的通行证。如果由于某种原因,您希望在每次过滤之后都保留指向每个备份的指针,那么在每次过滤之后,请在这些原始参考上放一个分支或标记。或者在过滤之前删除标签/分支,或者仅使用reflog。


I have an existing repository with folders and files on Github and I have just created four new repositories.

I wish to distribute the files/folders in the existing repository into the new repositories I have just created.

Is there a way I can do this using either the GUI or CMD (Macbook)?

解决方案

I don't know if there's any way to do it with a GUI.

If the things you want to extract are contained in their own folders, it's pretty easy on the command line. One method using git filter-branch --subdirectory-filter is explained here:

https://stackoverflow.com/a/12818479/955926

If your files and folders are not so nicely contained, or don't match a simple pattern (i.e. keep only the *.py files, etc), one method - which I call 'grooming' - is to just keep running simple filter-branch commands - on a clone, not the original! - to keep stripping away parts of it, until your history only contains what you want.

Of course, we always want to be able to verify what we're doing. This will show a sorted, uniqued listing of all the relative file/folder paths that have ever existed in your repo:

$ git log --pretty='format:' --name-only | grep -v '^$' | sort -u

Let's say in that list you see foo and bar/baz folders that you want gone from history (i.e. get rid of baz, which is in bar, and foo):

$ git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch foo bar/baz'

Now you can run the previous log command to verify that those links don't exist anywhere in time, and choose more things from that smaller list to get rid of. Or you can stick every path into that one call to filter-branch. I just feel better taking smaller steps and verifying, in the same way that I'm more comfortable committing granularly.

Filter-branch makes a backup each time through, and the second run would be halted by its existence. That's what the -f is for. You'll be overwriting your backup each time you filter again, but it's okay, because you're doing this on a clone (right!?), and you can always do it again on a new clone if you screw up very badly.

In the end you'll want to clear out the refs/original/* heads, which will point to the last head of the branch you're filtering after each pass. If you filter-branched the master branch, you'd have a refs/original/refs/heads/master file pointing to the old head of master, pre-filtering. When you force the next pass, you'll be pointing to that to the new one. If for some reason you want to keep pointers to each backup after each filtering, just drop a branch or tag at those 'original' refs after each pass. Or drop a tag/branch before filtering, or just use the reflog.

这篇关于将文件/文件夹从一个git仓库移到另一个仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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