非交互式方式来压缩一系列git提交,而不是从HEAD开始

问题描述 投票:1回答:1

我目前正在开发一个iOS项目。我有一个脚本,在我构建时运行。此脚本更新内部版本号并将其提交到git并将其推送。然而,在使用它几天后,我意识到副作用。我可以连续提交增加内部版本号的提交。所以我的提交日志充斥着这些提交。由于该项目被分解为几个核心库,因此这一点变得特别恶化。因此,在lib上工作总是会导致构建增量(因为它是相同的工作空间)。

我现在意识到,我需要一个足够聪明的脚本来压缩连续提交,这是更新构建号。

运行

git log -20 --pretty=format:"%h - %s"

例如,将屈服

6add12a1 - Update the package from handle
70f438be - Updated client version to 0.15.0.2152 and next version to 0.15.0.2153
7d84151f - Updated client version to 0.15.0.2151 and next version to 0.15.0.2152
eace113b - Updated client version to 0.15.0.2150 and next version to 0.15.0.2151
e72624dd - Updated client version to 0.15.0.2149 and next version to 0.15.0.2150
85d15b6c - Updated client version to 0.15.0.2148 and next version to 0.15.0.2149
a4e140cd - Updated client version to 0.15.0.2147 and next version to 0.15.0.2148
ffb18892 - Updated client version to 0.15.0.2146 and next version to 0.15.0.2147
ebd33432 - Updated client version to 0.15.0.2145 and next version to 0.15.0.2146
f0727ca7 - Updated client version to 0.15.0.2144 and next version to 0.15.0.2145
80ab2939 - Adjust balance if FB rewards popup is shown
0c04a1d7 - Updated client version to 0.15.0.2143 and next version to 0.15.0.2144
e89fd769 - Updated client version to 0.15.0.2142 and next version to 0.15.0.2143
c404f9ce - Updated client version to 0.15.0.2141 and next version to 0.15.0.2142
3911fb31 - Updated client version to 0.15.0.2140 and next version to 0.15.0.2141
ee3b7056 - Updated client version to 0.15.0.2139 and next version to 0.15.0.2140
cfb3b2a7 - Updated client version to 0.15.0.2138 and next version to 0.15.0.2139
a11fa3d9 - Add conditionals compilation to remove some logging
b9d0f75a - Disable backlight
ba4447ae - Updated client version to 0.15.0.2137 and next version to 0.15.0.2138

所以在这个例子中,我想要将70f438be压缩到f0727ca7,然后将提交更新为“将客户端版本从0.15.0.2138更新为0.15.0.2152”。

有两件事需要做。

  • 编写脚本或手动压缩连续的构建号增量提交
  • 更新我的脚本以检查先前的提交是否是构建号增量,如果是,则压缩。

我自己编写脚本没有问题。相反,这个问题的焦点是git命令本身。有关脚本的详细信息作为上下文提供。

我正在寻找的是如何非交互式地使用git来压缩未在HEAD开始的特定连续提交范围。到目前为止,我所看到的参考文献都从HEAD中被压扁了。

比如这个。

Is there a way to squash a number of commits non-interactively?

我将在今天晚些时候再讨论一下,因为它可能仍然有答案。

哦是的,澄清一下,这些提交都已经被推了。

请注意,这是在Mac上。后一个脚本是从Xcode(Build Phase)运行的,是一个使用GitPython的Python脚本。

git git-rebase
1个回答
1
投票

好的,所以我做了一些实验并运行了一段时间,我想出了一个解决方案。

如果那些有更好的git-fu的人能够证实这确实是一个好的/安全的方式,那将会有所帮助。

我放弃了尝试更改提交消息以反映更改。我确实找到了一种方法,但是由于我现在对存储库的所有功能有了更好的理解,我选择不担心这个细节。

这似乎是这个命令可以用来压缩一系列不是从HEAD开始的提交。

git rebase -Xtheirs --onto 80ab2939 7d84151f

这里的关键是-Xtheirs,因为至少在我的申请中,否则会导致冲突。注意我的情况很特殊,因为它依赖于已知的提交消息模式,并且文件是相同的。

我在这段历史上跑了以上

6add12a1 - Update the package from handle
70f438be - Updated client version to 0.15.0.2152 and next version to 0.15.0.2153
7d84151f - Updated client version to 0.15.0.2151 and next version to 0.15.0.2152
eace113b - Updated client version to 0.15.0.2150 and next version to 0.15.0.2151
e72624dd - Updated client version to 0.15.0.2149 and next version to 0.15.0.2150
85d15b6c - Updated client version to 0.15.0.2148 and next version to 0.15.0.2149
a4e140cd - Updated client version to 0.15.0.2147 and next version to 0.15.0.2148
ffb18892 - Updated client version to 0.15.0.2146 and next version to 0.15.0.2147
ebd33432 - Updated client version to 0.15.0.2145 and next version to 0.15.0.2146
f0727ca7 - Updated client version to 0.15.0.2144 and next version to 0.15.0.2145
80ab2939 - Adjust balance if FB rewards popup is shown
0c04a1d7 - Updated client version to 0.15.0.2143 and next version to 0.15.0.2144

这会产生一些输出:

First, rewinding head to replay your work on top of it...
Auto-merging resources/plists/jks-info.plist
Auto-merging resources/next_build_version.txt
[detached HEAD 8beac4c7] Updated client version to 0.15.0.2152 and next version to 0.15.0.2153
 Date: Mon Mar 4 02:01:15 2019 -0800
 2 files changed, 2 insertions(+), 2 deletions(-)
Committed: 0001 Updated client version to 0.15.0.2152 and next version to 0.15.0.2153
[detached HEAD 4ede3d58] Update the package from handle
 Date: Mon Mar 4 02:03:09 2019 -0800
 1 file changed, 4 insertions(+), 2 deletions(-)
Committed: 0002 Update the package from handle
All done

但由此产生的历史(git log)是好的。

4ede3d58 - Update the package from handle
8beac4c7 - Updated client version to 0.15.0.2152 and next version to 0.15.0.2153
80ab2939 - Adjust balance if FB rewards popup is shown
0c04a1d7 - Updated client version to 0.15.0.2143 and next version to 0.15.0.2144
e89fd769 - Updated client version to 0.15.0.2142 and next version to 0.15.0.2143

我的最后一步是强制推送,因为提交已被推送。请注意,强制推送将重写您的历史记录。我实际上使用git push --force-with-lease是安全的。

© www.soinside.com 2019 - 2024. All rights reserved.