gpt4 book ai didi

git - 有没有一种简单的方法可以压缩 git 中的许多提交?

转载 作者:行者123 更新时间:2023-12-02 02:33:09 24 4
gpt4 key购买 nike

我有以下问题:
我有很多提交需要压缩(几千个,我们有一个失控的脚本)。这些提交分组在 40 到 200 之间。
使用 git rebase -i 是不可行的,因为它会涉及太多的工作。我们有一个工具,可以输出相对于分支 HEAD 的此类组的第一个和最后一个提交(可用于通过其引用哈希获取实际提交)。

作为一个例子,我正在寻找可以将 HEAD~400 压缩为 HEAD~200 到单个提交中的东西。然后可以再次运行(使用更改参数)将 HEAD~100 压缩为 HEAD~50 到另一个单独的提交中。

编辑1:
我考虑过创建一个“假”编辑器,它本质上是通过对 rebase 文件进行更改来伪造交互性。一个抽象的示例脚本如下所示(我可以循环直到所有组都被压扁):

start=$('get start of oldest group')
end=$('get end of oldest group')
git config core.editor "'~/fakeeditor' -start $start -end $end"
git rebase -i

最佳答案

我个人最喜欢使用git reset --soft

所以,假设您想从 HEAD~1000 压缩到这一点(HEAD~1000 是最后一个幸存赢得胜利的提交不要被压扁):

git reset --soft HEAD~1000
git commit -m "Squashed a lot of stuff"

就是这样。您可以使用修订 ID,而不是使用 HEAD~n 引用。

我看到你想像分段那样做...所以,说...让我们将 HEAD~400 压缩到 HEAD~200...然后从 HEAD~200 压缩到 HEAD~100...然后从 HEAD~100 到 HEAD。因此,让我们创建一个临时分支来完成我们的工作。

git checkout -b temp HEAD~200
git reset --soft HEAD~400
git commit -m "squashing first segment"
# next segment
git checkout the-original-branch~100
git reset --soft temp
git commit -m "Second segment"
git branch -f temp #set temp over here
# final segment
git checkout --detach the-original-branch
git reset --soft temp
git commit -m "Final segment"
git branch -f temp #set temp over here
# if you like the result, feel free to move whatever branch over here
git branch -f whatever-branch
# and delete temp if so you want
git branch -D temp

我认为非常简单。

一天后,我刚刚意识到(通过回答另一个问题)可以用一种更简单的方式来完成:

git branch -f temp $( git commit-tree -p HEAD~400 -m "first squash" HEAD~200^{tree} )
# that set up temp on the first squash
git branch -f temp $( git commit-tree -p temp -m "second squash" HEAD~100^{tree} )
# temp has move to second squash
git branch -f temp $( git commit-tree -p temp -m "final squash" HEAD^{tree} )

现在 temp 已经按照我的示例中请求的方式压缩了提交。请随意在那里执行 reset --hard(使用 reset --hard 时的常见警告)。

关于git - 有没有一种简单的方法可以压缩 git 中的许多提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64758954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com