如何在Git仓库中移动目录以进行所有提交? [英] How can I move a directory in a Git repo for all commits?

查看:245
本文介绍了如何在Git仓库中移动目录以进行所有提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含这个目录结构的仓库:

  repo / 
blog /
_posts /
some-post.html
another-file.txt

我想将 _posts 移动到回购顶层,因此结构如下所示:

  repo / 
_posts /
some-post.html
another-file.txt

git mv 很简单,但我想让历史看起来像 _posts always 存在于repo的根目录下,我希望能够获取 some-post.html 通过 git log - _posts / some-post.html 。我想我可以用 git filter-branch 来实现这个功能,但我还没有弄清楚如何做到这一点。任何想法?

解决方案

您可以使用子目录过滤器来实现此功能

  $ git filter-branch --subdirectory-filter blog /  -  --all 

编辑1:如果您不想有效地制作 _posts 根目录,请改用树形过滤器:

  $ git filter-branch --tree-filter'mv blog / _posts。'HEAD 
如果 blog / _posts 没有存在于某些提交中,上述将失败。改为使用它:

  $ git filter-branch --tree-filter'test -d blog / _posts&& mv博客/ _posts。 || echoNothing to doHEAD 


Let's say I have a repo that includes this directory structure:

repo/
  blog/
    _posts/
      some-post.html
  another-file.txt

I want to move _posts to the top level of the repo, so the structure will look like this:

repo/
  _posts/
    some-post.html
  another-file.txt

This is simple enough with git mv, but I want to make the history look as though _posts always existed at the root of the repo, and I want to be able to get the entire history of some-post.html via git log -- _posts/some-post.html. I imagine I can use some magic with git filter-branch to accomplish this, but I haven't figured out exactly how to do that. Any ideas?

解决方案

You can use the subdirectory filter to achieve this

 $ git filter-branch --subdirectory-filter blog/ -- --all

EDIT 1: If you don't want to effectively make _posts the root, use a tree-filter instead:

 $ git filter-branch --tree-filter 'mv blog/_posts .' HEAD

EDIT 2: If blog/_posts did not exist in some of the commits, the above will fail. Use this instead:

 $ git filter-branch --tree-filter 'test -d blog/_posts && mv blog/_posts . || echo "Nothing to do"' HEAD

这篇关于如何在Git仓库中移动目录以进行所有提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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