git-filter-branch:保持目录结构不变 [英] git-filter-branch: leave directory structure unchanged

查看:110
本文介绍了git-filter-branch:保持目录结构不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的存储库中有一个目录结构:

I have a directory structure in my repository:

|-repository/
|  |  |-repository/fileOne
|  |
|  |-subOne/
|  |  |-subOne/fileTwo
|  |
|  |-subTwo/
|     |-subTwo/fileThree

我想将目录subOne移到另一个存储库中.为此,我像这样使用git-filter-branch分隔subOne:

I want to move the directory subOne into a different repository. To do that, I'm separating subOne using git-filter-branch like so:

$ git filter-branch --subdirectory-filter subOne --prune-empty

这给我留下了如下分支历史记录:

This leaves me with a branch history that looks like this:

$ git log --oneline
0c0ea11 subOne history
6318bba subOne history
c96fddb Initial commit

这正是我想要的;在另一个存储库中,我可以执行git fetchgit merge,这会将subOne和历史记录添加到另一个存储库中.太好了.

This is exactly what I want; from another repository I can do a git fetch and git merge, which adds subOne and history into the other repository. That's great.

但是,git-filter-branch已删除了subOne.检查我分支上的目录结构:

However, git-filter-branch has removed subOne. Checking the directory structure on my branch:

$ ls
fileOne
$ pwd
/this/is/a/path/repository

pwd 读取:/this/is/a/path/repository,但是ls应在存储库中显示subOne.相反,我得到的是subOne中的文件向上移动,并删除了目录.

pwd should read: /this/is/a/path/repository, but ls should show subOne inside the repository. What I've got instead is the files from subOne moved upwards, and the directory removed.

如何在不丢失目录的情况下提取subOne?

How can I extract subOne without losing the directory?

推荐答案

pwd应该显示为:/this/is/a/path/repository,但是ls应该在存储库中显示subOne.相反,我得到的是subOne中的文件向上移动,并删除了目录.

pwd should read: /this/is/a/path/repository, but ls should show subOne inside the repository. What I've got instead is the files from subOne moved upwards, and the directory removed.

事实证明,这是git-filter-branch --subdirectory-filter默认操作.引用man git-filter-branch:

It turns out this is the default operation of git-filter-branch --subdirectory-filter. Quoting from man git-filter-branch:

结果将包含该目录(并且仅包含该目录)作为其项目根目录.

The result will contain that directory (and only that) as its project root.

要解决此问题,我想将/中的所有内容移至/subdirectory.幸运的是,在man git-filter-branch中,在EXAMPLES下有一段看似伏都教的代码:

To fix this, I want to shift everything in / to /subdirectory. Luckily, there's a piece of voodoo-looking code in man git-filter-branch, under EXAMPLES:

To move the whole tree into a subdirectory, or remove it from there:
       git filter-branch --index-filter \
               'git ls-files -s | sed "s-\t\"*-&newsubdir/-" |
                       GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                               git update-index --index-info &&
                mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD

在Mac OS X上将无法使用.请参阅我的问题此处,以获取更多信息. BSD sedGNU sed不同,这很烦人.要在OS X上运行该示例,请将sed更改为gsed;过滤器就可以很好地工作了.

THIS WILL NOT WORK ON MAC OS X. See my question here for more information. BSD sed is not the same as GNU sed, which is annoying. To run that example on OS X, change sed to gsed; the filter then works quite well.

这篇关于git-filter-branch:保持目录结构不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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