简体   繁体   English

压榨master分支的历史记录,但将所有先前的提交消息归咎于此?

[英]Squash master branch history but keep all prior commit messages in blame?

I have been researching git squash but am unsure if it would even apply to what I am trying to do. 我一直在研究git南瓜,但是不确定它是否适用于我尝试做的事情。 I only know the basics of git so maybe this is a ridiculous thing to do. 我只知道git的基础,所以也许这是一件荒谬的事情。

I have a master branch that has lets say 10,000 commits on it. 我有一个主分支,可以说有10,000次提交。 Lets assume it looks like this: 让我们假设它看起来像这样:

1-2-3...5000...9999-10000

Lets assume all files in the repo have been modified at some points in time. 假设仓库中的所有文件在某些​​时间点已被修改。 For example, "test.php" file has commits at 1,2,3,2500,6000 . 例如,“ test.php”文件的提交值为1,2,3,2500,6000

Now what I want to do is to make the entire history of the master branch start at commit 5000 but keep the commit logs intact? 现在,我要做的是使master分支的整个历史记录始于commit 5000,但保持提交日志不变? Is this even possible? 这有可能吗?

For example using blame on "test.php": 例如,将责任归咎于“ test.php”:

<commit 1> | echo 'hello world';
<commit 2> | echo 'another line in the file';
<commit 6000> | echo 'sometime later';

My thought of why I want to do this is simple. 我对为什么要这样做的想法很简单。 At this point I will never rollback the code beyond commit 5000 but it would be great to see who did the change. 在这一点上,我将永远不会回退超过commit 5000的代码,但是很高兴看到谁进行了更改。 It will also reduce the size of a checkout which at this point is very large. 这也将减小结帐的大小,这在当时非常大。

Git gets the information it shows when annotating lines in source files from the commit history. 当从提交历史记录注释源文件中的行时,Git会获取显示的信息。 So if you get rid of parts of the commit history, that information isn't available to git anymore. 因此,如果您删除了部分提交历史记录,那么该信息将不再适用于git。

Just because you don't want to roll back before some commit isn't a reason to give up the history. 仅仅因为您不想在进行某些提交之前就回滚并不是放弃历史的原因。 There are many more reasons to keep it, with git annotate being just one of them. 保留它还有更多的原因, git annotate只是其中之一。

So the only problem you are trying to solve seems to be the amount of data that needs to be transferred when cloning. 因此,您要解决的唯一问题似乎是克隆时需要传输的数据量。 You can reduce this by using the --depth option to git clone to create a shallow clone. 您可以通过使用--depth选项对git clone创建浅表克隆来减少这种情况。 This way, the history will still be available in some remote, but you choose yourself how much of the history you want to copy to your clone. 这样,历史记录仍将在某些远程服务器上可用,但是您可以选择要复制到克隆中的历史记录数量。

A shallow clone is also a good way to determine how much space you could save by squashing the history. 浅表克隆也是确定通过压缩历史记录可以节省多少空间的好方法。 Note that --depth saves space in two different ways: It clones only the single branch HEAD on the remote is currently pointing to, and it clones that branch only to a certain depth. 请注意,-- --depth通过两种不同的方式节省空间:它仅克隆远程当前指向的单个分支HEAD ,并且仅将该分支克隆到特定深度。 You can use the --no-single-branch option in addtion to get more comparable numbers to judge whether it's worth it to squash the history. 您还可以使用--no-single-branch选项来获得更多可比较的数字,以判断是否值得挤压历史记录。 Most often, it's not. 多数情况并非如此。

To test the effect of --depth locally, you could do 要在本地测试--depth的效果,您可以

git clone --no-local --no-hardlinks --no-single-branch --depth 100 path/to/repository path/to/clone

This will create a shallow clone of your local repository while overriding the usual local optimisations. 这将创建本地存储库的浅表克隆,同时覆盖通常的本地优化。 You can then compare the total space consumption using 然后,您可以使用

du -sm path/to/repository
du -sm path/to/clone

Squashing commits will rewrite history and you will be the one who made that change and you will be the one appearing in blame. 压缩提交将重写历史记录, 将是做出更改的人,而将是受到指责的人。 If a thousand people made changes and one person decided to squish all of those changes into one big change, who made that one big change? 如果有一千个人进行了更改,而一个人决定将所有这些更改压缩为一个大更改,那么谁做了那个大更改? The one person who squished everything. 压榨一切的人。 Git forgot about changes beyond that because the history was rewritten. Git忘记了其他更改,因为历史被重写了。

In other words, that is not possible. 换句话说,这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM