【发布时间】:2014-07-07 16:49:50
【问题描述】:
我想从 GitHub 上的提交历史中删除一些提交。
但是,我不想实际删除与特定提交相关的代码。我只想从提交历史记录中删除提交。这可能吗?如果有,怎么做?
谢谢!
【问题讨论】:
-
也许 rebase 并将有问题的提交压缩到另一个?
-
感谢 pdobb!注意举个例子,因为那样会更容易。
我想从 GitHub 上的提交历史中删除一些提交。
但是,我不想实际删除与特定提交相关的代码。我只想从提交历史记录中删除提交。这可能吗?如果有,怎么做?
谢谢!
【问题讨论】:
您需要的是压缩提交。
您想查看this article 以了解更多信息
假设您有 3 个提交要转换为 1 个,因为所有这些提交实际上都应该是一个单独的提交,因此您希望拥有新功能提交和许可证提交。它们以相反的顺序出现(第一个提交是最旧的)
你首先要重新定位你当前的分支
$ git rebase -i HEAD~4
pick 9420b31 New feature
pick 8414d83 Adding license to code
pick 7bfb349 More info into the license
pick c79e70f Fixing license
# Rebase 93275f0..9420b31 onto 93275f0
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
然后将许可证提交从“pick”更改为“fixup”(如果您想丢弃提交消息)或“squash”(如果您需要保留它)。
在这个例子中,那将变成
$ git rebase -i HEAD~4
pick 9420b31 New feature
pick 8414d83 Adding license to code
fixup 7bfb349 More info into the license
fixup c79e70f Fixing license
# Rebase 93275f0..9420b31 onto 93275f0
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
之后,您将只有两个提交:一个添加功能,另一个添加许可证(但是,表示许可证提交的哈希值会改变)。
请注意:如果您已经将历史推送到远程服务器,您可能需要“推送 --force”它们。如果有人克隆了那个 repo,他们在更新时可能会遇到问题(冲突)。
【讨论】: