【问题标题】:how to merge different commit in git log history如何在 git log 历史记录中合并不同的提交
【发布时间】:2013-03-24 21:27:47
【问题描述】:

假设我们有提交 1->2->3->4->5 我认为历史可能太长了,我希望日志将 1、2 和 3 合并为一个,例如 1'->2->3->4->5 我该怎么做,我应该使用 rebase 吗?似乎我无法在 rebase 后推送到远程仓库。 提前致谢。

【问题讨论】:

  • 使用交互式变基,并确保在推送任何提交后不要这样做。检查此链接:git-scm.com/book/en/…
  • 我认为它比简单的“在你推送任何提交后不要使用 rebase”更微妙。我认为如果它是一个私人分支是很好的。但是@user1615903 是正确的,如果它与其他人共享并且预计其他人会定期提取该分支(例如,master),那么您不想重写历史记录——这就是 rebase 所做的。

标签: git commit rebase


【解决方案1】:

首先进入交互变基模式

git rebase -i HEAD~5

(将 5 替换为您想要压缩的提交数)

然后按照 git book Squashing commmits -part 中的说明进行操作。

From the git book:

也可以进行一系列提交并将它们压扁 使用交互式变基工具进行一次提交。剧本 在变基消息中提供有用的说明:

#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

如果您指定“squash”而不是“pick”或“edit”,则应用 Git 那个变化和它之前的变化都让你合并 提交消息在一起。所以,如果你想进行一次提交 从这三个提交中,您可以使脚本如下所示:

pick f7f3f6d changed my name a bit
squash 310154e updated README formatting and added blame
squash a5f4a0d added cat-file

当您保存并退出编辑器时,Git 会应用所有三个更改并 然后将您带回编辑器以合并三个提交消息:

# This is a combination of 3 commits.
# The first commit's message is:
changed my name a bit

# This is the 2nd commit message:

updated README formatting and added blame

# This is the 3rd commit message:

added cat-file

当你保存它时,你有一个提交,它引入了 所有三个先前提交的更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多