git 变基后发生新提交时该怎么办?

What to do when new commits happen after a git rebase?

我刚刚完成了一个特别棘手的 rebase(有人在一个分支上工作了数周而从未进行过 rebase。)我花了大约两三个小时,因为我使用的 "human readable" 格式只是一个一堆 ID 和引用。

当我做这个 rebase 时,在我有机会 git 推送 rebase 的结果之前,分支上又出现了两个提交。

是否有最佳实践来获得这些新提交而无需重新进行变基或诉诸合并?我最初的想法是我可以 git cherry-pick 那些新提交并 git push -f,但这会不会令人讨厌?

您应该能够再次变基您的提交。

唯一的问题是您将不得不重新制定冲突解决方案,因为您没有激活 git rerere

好吧,...你不必,有了 rerere-train.sh script
参见“Do you even rerere?" by Tristan Roussel:

In this simplest form, the script starts from the commit you specified and go through every parent commit to look for conflicts.

这将允许您记录那些过去的冲突解决方案,并且无需再次执行它们就可以再次变基。


随着 Git 2.36(2022 年第 2 季度),rerere-train 脚本(在 contrib/ 中)得到改进。

参见 commit 2587df6 (27 Feb 2022) by Junio C Hamano (gitster)
(由 Junio C Hamano -- gitster -- in commit 50e0dd8 合并,2022 年 3 月 6 日)

rerere-train: two fixes to the use of "git show -s"

The script uses "git show -s"(man) to display the title of the merge commit being studied, without explicitly disabling the pager, which is not a safe thing to do in a script.

For example, when the pager is set to "less" with "-SF" options (-S tells the pager not to fold lines but allow horizontal scrolling to show the overly long lines, -F tells the pager not to wait if the output in its entirety is shown on a single page), and the title of the merge commit is longer than the width of the terminal, the pager will wait until the end-user tells it to quit after showing the single line.

Explicitly disable the pager with this "git show"(man) invocation to fix this.

The command uses the "--pretty=format:..." format, which adds LF in between each pair of commits it outputs, which means that the label for the merge being learned from will be followed by the next message on the same line.
"--pretty=tformat:..." is what we should instead, which adds LF after each commit, or a more modern way to spell it, i.e.
"--format=...".
This existing breakage becomes easier to see, now we no longer use the pager.

cherry-pick 仅用于合并特定的提交,但如果您的分支包含许多需要合并的提交,那么最好 rebase 它。

不同的人可能有不同的最佳做法。我遵循以下内容:

  1. git pull --rebase 这样我就可以确保我的分支从它创建的远程分支是最新的。
  2. git push origin :feature_branch 这将删除删除远程功能分支。虽然我知道我可以简单地做 git push -force feature_branch 但我想确认 git 不会弄乱任何东西:) :) :)
  3. 终于git push origin feature_branch

我很高兴知道你的做法或建议我更好的东西。