Git 仅提取本地项目子目录的历史记录,并使其成为另一个本地项目的存储库

Git 仅提取本地项目子目录的历史记录,并使其成为另一个本地项目的存储库,git,Git,我有一个项目是从一个目录开始的。然后,我向它添加了目录,但用与子项目匹配的子目录构建了主项目。其中一个子目录是,比如说lib/,它包含了基于相同体系结构的所有项目的共同点 由于lib/已经成为一个值得拥有自己的GIT(子)树的项目,我想让它独立,但我不想丢失在主项目中工作时所做的所有相关提交。我想要的是一个我的主存储库的副本,以及它的整个提交历史记录,除了lib/中的文件外,所有内容都被剥离 所以我就这么做了 我必须承认,我并不总是很好地理解吉特的行话,所以,请容忍我 我做了git clone-

我有一个项目是从一个目录开始的。然后,我向它添加了目录,但用与子项目匹配的子目录构建了主项目。其中一个子目录是,比如说
lib/
,它包含了基于相同体系结构的所有项目的共同点

由于
lib/
已经成为一个值得拥有自己的GIT(子)树的项目,我想让它独立,但我不想丢失在主项目中工作时所做的所有相关提交。我想要的是一个我的主存储库的副本,以及它的整个提交历史记录,除了
lib/
中的文件外,所有内容都被剥离

所以我就这么做了

我必须承认,我并不总是很好地理解吉特的行话,所以,请容忍我

我做了
git clone-l lib
,然后从目录
lib/
运行
git filter branch-f--prune empty
。我运行了
git status
,它告诉我来源和这个“分支”不同,我应该运行
git pull
。。。六羟甲基三聚氰胺六甲醚。。。我只使用本地存储库,所以我尝试了
git remote rm origin
,消息消失了。但是我想有一条捷径可以避免这种情况,对吗

无论如何,我在日志树中看到的现在都是提交。。。或者什么东西,不管是三重的:

$ git log --reflog --graph --oneline --decorate --date-order
* 880d3e8 Framework Library - Update
| * 2cfbb42 (refs/original/refs/heads/1.0) Framework Library - Update
| | * 578968f (HEAD -> 1.0) Framework Library - Update
* | | 65daea4 Tools: ECU simulator (new)
| * | 62981c7 Tools: ECU simulator (new)
| | * 9e4015d Build 423 - Makefile bugfixes and small changes
* | | 3eddb88 Build 423 - Makefile bugfixes and small changes
| * | 82b5ed1 Build 423 - Makefile bugfixes and small changes
* | | bb46ee9 Build 423 - Bugfixes
| * | 7cd40ac Build 423 - Bugfixes
* | | ab0058c Build 420 - Bugfixes
| * | 3f3257b Build 420 - Bugfixes
| | * 2f2184f Build 416 - Enhancements and fixes
* | | 39ea1de Build 416 - Enhancements and fixes
| * | 11c1f0f Build 416 - Enhancements and fixes
| | * 770d628 Build 406 - Enhancements
* | | 952f9a2 Build 406 - Enhancements
| * | f0c86c3 Build 406 - Enhancements
| | * 5b8cfef Build 405 - Bugfixes and enhancements
* | | 6c1b590 Build 405 - Bugfixes and enhancements
| * | 0e79341 Build 405 - Bugfixes and enhancements
...
这正常吗?我如何修剪多余的

我只使用本地存储库,不打算很快使用远程存储库。当然,除非我遗漏了什么


哦,我也有备份。(如果只是一个…

git filter branch所做的是重写历史记录,即在没有过滤掉的内容的情况下重新创建新提交

因此,您可能会看到三个副本的原因是,在筛选器分支之后仍然存在旧的历史记录行。在prune设置为now或all的情况下运行垃圾收集器应该可以解决这个问题

git gc--prune=now


我想我已经开始理解GIT是如何工作的了——对,迟做总比不做强。原来我所要做的就是另一个
克隆

$ git clone lib lib-new
$ cd lib-new
$ git remote rm origin
$ git log --reflog --graph --oneline --decorate --date-order
* 578968f (HEAD -> 1.0) Framework Library - Update
* 9e4015d Build 423 - Makefile bugfixes and small changes
* 2f2184f Build 416 - Enhancements and fixes
* 770d628 Build 406 - Enhancements
* 5b8cfef Build 405 - Bugfixes and enhancements
* 44421b9 Intermediate build - Added `wait()` function template to class `Scheduler`
* 5fdc840 Build 395 - Bugfixes and enhancements
* c8b34e1 Build 375 - Bugfixes
* 12cb53f Build 371 - Bugfixes and enhancements
* 981d3f8 Build 360 - Enhancements
* f5127b6 Build 356 - Major bugfix
...
要一次性总结所有操作,请执行以下操作:

# From the parent directory
git clone -l project-with-lib lib-temp
cd lib-temp

# Detach from the origin:
git remote rm origin

git filter-branch -f 'rm <list of unwanted files/directories>'
git filter-branch -f --prune-empty
cd ..
git clone -l lib-temp lib

# Detach from the origin:
cd lib
git remote rm origin

# Scrap the temporary work space:
cd ..
find lib-temp -delete
父目录中的
#
git clone-l项目的lib temp
光盘库温度
#从原点拆离:
git远程rm源
git筛选器分支-f'rm'
git筛选器分支-f--修剪为空
光盘
git克隆-l库临时库
#从原点拆离:
光盘库
git远程rm源
#废弃临时工作空间:
光盘
查找临时库-删除

第一次克隆操作创建了一个目录树,需要用作临时工作空间-我正在使用本地存储库,因此没有
git push
来自然实现这一点,它必须手动完成,因此第二次克隆操作需要进行房屋清理。

我尝试过这个,但“重复”了仍然存在。。。我做了另一个
git克隆
,副本现在不见了。很抱歉,这是我的错,你是对的,git gc不会做任何事情,我忽略了git日志将显示可从refs(HEAD,branchs)访问的提交的事实。这背后的原因很明显,过滤器分支会为您保留原始引用。如果您想恢复到旧状态,您可以在refs/original文件夹中找到它们。不要担心;-)。事实上,我记得GIT确实将删除作为引用,但实际上并没有删除任何内容。只有我刚刚理解的克隆操作通过只复制(我称之为)活动引用或活动引用来进行清理。