使用git filter-branch删除除文件列表以外的所有内容的历史记录 [英] Remove history for everything except a list of files using git filter-branch

查看:132
本文介绍了使用git filter-branch删除除文件列表以外的所有内容的历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个git存储库repo1repo2之间移动某些文件.我有一小段要移动的文件(保留历史记录).

I'm trying to move some files between two git repositories repo1 and repo2. I have a short list of files I'd like to move (preserving history).

要从repo1移动的三个文件:

libraryname/file1
libraryname/file2
tests/libraryname/file3

libraryname/tests/libraryname/中还有其他文件. /tests/

There are other files in libraryname/ and tests/libraryname/. There are other folders in / and tests/

我的计划是检出repo1,然后修改历史记录树,直到它仅包含我感兴趣的文件的历史记录.然后检出repo2,并合并到上一操作的输出中.看来git filter-branch是第一步的正确工具.

My plan is to checkout repo1, then modify the history tree until it only contains history for the files I'm interested in. Then checkout repo2, and merge in the output of the previous operation. It seems like git filter-branch is the right tool for the first step.

到目前为止,我已经尝试过git filter-branch --index-filter 'git rm -r --cached <FILES>' <FILES>列出每个不需要的整个文件夹或文件.

So far I've tried git filter-branch --index-filter 'git rm -r --cached <FILES>' Where <FILES> lists every unwanted whole folder or file.

但是,这留下了很多文件夹,这些文件夹在HEAD上不再存在,但在此存储库生存期内的某个时间点已经存在.弄清楚该回购历史中已存在的一切似乎很乏味-必须有更好的方法

But this leaves a lot of folders which no longer exist at HEAD, but have existed at some point in this repositories lifetime. It seems quite tedious to figure out everything that has existed in the history of this repo - there must be a better way

我如何最终得到仅包含这三个文件的git commit树? 有没有比我建议的更好的方法? 或者,是否可以删除HEAD当前不存在的所有文件的痕迹?

How do I end up with a git commit tree which only includes these three files? Is there a better way then I'm suggesting? Or, is there a way to remove traces of all files which don't currently exist at HEAD?

推荐答案

您说它留在文件夹后面;我认为您的意思是它在这些文件夹中留下了文件 (因为git不会保留空文件夹)...

You said it leaves behind folders; I assume you mean it leaves behind files in those folders (because git doesn't preserve empty folders)...

似乎您可能想采用清除索引,然后重新添加所需条目的方法.

It seems like you might want to take the approach of clearing the index and then re-adding the entries you want.

git filter-branch ...
    --index-filter 'git rm -r --cached * && git reset $GIT_COMMIT -- libraryname/file1 libraryname/file2 tests/libraryname/file3
    ...

由于您将内容精简了很多,所以请不要忘记您可能要包含--prune-empty选项

Since you're thinning out the content so much, don't forget that you may want to include a --prune-empty option

这篇关于使用git filter-branch删除除文件列表以外的所有内容的历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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