如何在保留子目录的同时拆分git存储库? [英] How to split a git repository while preserving subdirectories?

查看:122
本文介绍了如何在保留子目录的同时拆分git存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的与这个问题类似。但是,我希望拆分为独立回购的目录保留在该回购的子目录中:



我有这个:

  foo / 
.git /
bar /
baz /
qux /


 

foo /
.git /
bar /
baz /

quux /
.git /
qux / #注意:仍然是一个子目录

如何在git中执行此操作?



我可以使用这个答案如果在整个历史过程中有一些方法可以将所有新回购的内容移动到一个子目录中。

解决方案

使用子目录过滤器,然后使用索引过滤器将内容放回到子目录中,但为什么打扰,何时可以使用强制t使用索引过滤器本身是什么?



以下是手册页中的示例:

  git filter-branch --index-filter'git rm --cached --ignore-unmatch filename'HEAD 

这只是删除一个文件名;你想要做的是删除除了给定子目录以外的所有内容。如果你想保持谨慎,你可以明确列出每条路径,但是如果你想要全押,你可以做这样的事情:

  git filter-branch --index-filter'git ls-tree -z --name-only --full-tree $ GIT_COMMIT | grep -zv^ directory-to-keep $| xargs -0 git rm --cached -r' -  --all 

我预计可能会有更优雅的方式;

关于该命令的一些注释:


    li> filter-branch内部设置GIT_COMMIT为当前提交SHA1
  • 我不希望 - full-tree 为但很显然,filter-branch运行从 .git-rewrite / t 目录中的索引过滤器,而不是回购的最高级别。

  • grep可能是矫枉过正,但我​​认为这不是速度问题。 所有裁判;我想你真的想要这样。 ( - 将它从filter-branch选项中分离出来)

  • -z -0 告诉ls-tree,grep和xargs使用NUL终止来处理文件名中的空格。



编辑,稍后:Thomas有用地建议一种方法来删除现在空的提交,但现在已过时。看看编辑历史,如果你有一个老版本的git,但使用现代git,你需要做的就是添加这个选项:

   - 修剪空白

这将删除所有空的提交索引过滤器的应用。

What I want is similar to this question. However, I want the directory that is split into a separate repo to remain a subdirectory in that repo:

I have this:

foo/
  .git/
  bar/
  baz/
  qux/

And I want to split it into two completely independent repositories:

foo/
  .git/
  bar/
  baz/

quux/
  .git/
  qux/  # Note: still a subdirectory

How to do this in git?

I could use the method from this answer if there is some way to move all the new repo's contents into a subdirectory, throughout history.

解决方案

You could indeed use the subdirectory filter followed by an index filter to put the contents back into a subdirectory, but why bother, when you could just use the index filter by itself?

Here's an example from the man page:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

This just removes one filename; what you want to do is remove everything but a given subdirectory. If you want to be cautious, you could explicitly list each path to remove, but if you want to just go all-in, you can just do something like this:

git filter-branch --index-filter 'git ls-tree -z --name-only --full-tree $GIT_COMMIT | grep -zv "^directory-to-keep$" | xargs -0 git rm --cached -r' -- --all

I expect there's probably a more elegant way; if anyone has something please suggest it!

A few notes on that command:

  • filter-branch internally sets GIT_COMMIT to the current commit SHA1
  • I wouldn't have expected --full-tree to be necessary, but apparently filter-branch runs the index-filter from the .git-rewrite/t directory instead of the top level of the repo.
  • grep is probably overkill, but I don't think it's a speed issue.
  • --all applies this to all refs; I figure you really do want that. (the -- separates it from the filter-branch options)
  • -z and -0 tell ls-tree, grep, and xargs to use NUL termination to handle spaces in filenames.

Edit, much later: Thomas helpfully suggested a way to remove the now-empty commits, but it's now out of date. Look at the edit history if you've got an old version of git, but with modern git, all you need to do is tack on this option:

--prune-empty

That'll remove all commits which are empty after the application of the index filter.

这篇关于如何在保留子目录的同时拆分git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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