如何在保存历史的同时将文件从一个git存储库移动到另一个 [英] How to move a file from one git repository to another while preserving history

查看:167
本文介绍了如何在保存历史的同时将文件从一个git存储库移动到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个文件(称为foo.txt)从一个存储库移动到另一个(不相关的)存储库,以保留其历史记录。 ebneter的问题显示了如何为子目录执行此操作。 taw的问题有一些提示和建议,但不是一步一步遵循。 jkeating的问题 a>看起来很有希望,但对我没有用。谷歌搜索在这个特定用例中空了。我正在寻找的是一个明确的命令序列来完成这项任务。



我开始遵循的一系列命令是这样的:

  $ git克隆源码回购/源码回购拷贝
$ cd源码回购拷贝
$ git filter-branch --tree - 过滤器'测试! $ @=foo.txt&& \
git rm --cached --ignore-unmatch $ @ || true'--prune-empty

我的过滤命令的逻辑是 git rm 所有不是foo.txt的文件。我添加了 || true 命令强制它有一个零返回值来满足过滤分支。



我的目的是设置source-repo-复制为我的目标存储库(target-repo)的远程文件,并假设 git filter-branch 过滤掉了除foo.txt以外的所有内容,将source-repo-copy针对回购。不幸的是, git filter-branch 命令似乎没有效果。它运行时没有出现任何错误,似乎是通过源代码回购中的600多个提交进行研究,但是当它完成时, git log 和source-repo-copy中的文件看起来像相同。应该不是所有的文件,但foo.txt都会丢失,所有没有触及它的提交都会从日志中消失吗?



此时我不知道如何继续。任何建议吗?

解决方案

这对我有用,但带有整个目录。



此处所示

 〜$ cd neu 
〜/ neu $ git filter-branch --subdirectory-filter FooBar HEAD
〜/ neu $ git reset --hard
〜/ neu $ git remote rm origin
〜/ neu $ rm -r .git / refs / original /
〜/ neu $ git reflog expire --expire = now --all
〜/ neu $ git gc --ggressive
〜/ neu $ git prune
〜/ neu $ git remote add origin git://github.com/FooBar/neu.git

编辑:对于单个文件:

首先过滤目录:

  git filter-branch --prune-empty --subdirectory-filter myDirectory  -   - -all 

过滤单个文件:

<$ git过滤分支-f --prune空--index过滤器git rm --cached --ignore-unmatch $(git ls-fi

b
$ b

  git reset --hard 
git gc --ggressive
git prune



这应该做到。


I am trying to move a single file (call it foo.txt) from one repository to another (unrelated) repository, preserving its history. ebneter's question shows how to do this for a subdirectory. taw's question has some hints and suggestions, but not a step-by-step procedure to follow. jkeating's question looked promising, but didn't work for me. Google searches came up empty for this particular use case. What I am looking for is a clear sequence of commands to accomplish this.

The sequence of commands I started to follow was this:

$ git clone source-repo/ source-repo-copy
$ cd source-repo-copy
$ git filter-branch --tree-filter 'test ! "$@" = "foo.txt" && \
  git rm --cached --ignore-unmatch $@ || true' --prune-empty

The logic of my filter command is to git rm all files that are not foo.txt. I added the || true to the command to force it to have a zero return value to satisfy filter-branch.

My intention was to set source-repo-copy as a remote for my target repository (target-repo), and assuming git filter-branch filtered out everything but foo.txt, fetch and merge source-repo-copy into target-repo. Unfortunately, the git filter-branch command seemed to have no effect. It ran with no errors and appeared to grind through the 600+ commits in source-repo, but when it finished, the git log and files in source-repo-copy looked the same. Shouldn't all files but foo.txt be missing and all commits that didn't touch it be gone from the log?

At this point I don't know how to proceed. Any suggestions?

解决方案

This worked for me, but with a whole directory.

As shown here

~$ cd neu
~/neu$ git filter-branch --subdirectory-filter FooBar HEAD
~/neu$ git reset --hard
~/neu$ git remote rm origin
~/neu$ rm -r .git/refs/original/
~/neu$ git reflog expire --expire=now --all
~/neu$ git gc --aggressive
~/neu$ git prune
~/neu$ git remote add origin git://github.com/FooBar/neu.git

EDIT: For a single file:

Filter the directory first:

 git filter-branch --prune-empty --subdirectory-filter myDirectory -- --all

Filter the single file:

 git filter-branch -f --prune-empty --index-filter "git rm --cached --ignore-unmatch $(git ls-files | grep -v 'keepthisfile.txt')"

Do some cleanup:

 git reset --hard
 git gc --aggressive
 git prune

This should do it.

这篇关于如何在保存历史的同时将文件从一个git存储库移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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