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



我有一个从单个目录开始的项目。然后,我在其中添加了目录,但用匹配子项目的子目录结构了主项目。这些子目录之一是lib/,它包含我所有基于同一架构的项目的共同点。

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

所以我看到它可以完成。

我必须承认我并不总是很好地理解git行话,所以请忍受。

我做了git clone -l <main project> 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
...

那是正常的吗?我该如何修剪多余的?

我仅与本地存储库合作,并且不打算在短期内使用遥远的存储库。好吧,除非我错过了什么,否则。

哦,我 do 有备份。(如果只是一个...(

git filter-branch所做的是重写历史记录,即重新创建了新的提交,而无需过滤出来。

因此,您可能会看到三个副本的原因是由于过滤器分支后仍在那里。将垃圾收集器运行到现在,或者全部解决此问题。

git gc -prune =现在

我想我开始掌握git的工作原理 - 对,迟到总比没有好。原来我要做的就是另一个clone

$ 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 push可以自然地完成此操作,必须手工完成,因此第二个克隆操作,可以进行房屋清洁。

最新更新