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

时间:2017-06-27 17:42:38

标签: git

我有一个以单个目录开头的项目。然后我添加了目录,但是使用与子项目匹配的子目录构建了主项目。其中一个子目录是lib/,它包含了基于相同架构的所有项目的共同点。

由于lib/已经成为一个值得拥有自己的GIT(子)树的项目,我想让它独立但我不想丢失我所做的所有相关提交在主项目中工作。我喜欢的东西是我的主存储库的副本以及它的整个提交历史记录,其中包括lib/中的所有文件。

所以我saw can be done

我必须承认我并不总是非常了解GIT术语,所以,请耐心等待。

我做了git clone -l <main project> lib然后我从目录git filter-branch -f --prune-empty运行了lib/。我跑了git status它告诉我原点和这个&#34;分支&#34;不同,我应该运行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
...

这是正常的吗?如何修剪多余的?

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

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

2 个答案:

答案 0 :(得分:0)

git filter-branch所做的是重写历史记录,即重新创建新的提交而不会过滤掉你的东西。

因此,您可能会看到三个副本的原因是由于旧的历史记录行仍然存在于filter-branch之后。使用修剪设置为现在或全部运行垃圾收集器应该解决此问题。

  

git gc --prune = now

答案 1 :(得分:0)

我想我已经开始了解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来实现这一点,它必须用手完成,因此进行第二次克隆操作,进行房屋清洁。