扩展我在评论中写的内容
一般规则是您不应该重写(更改)您已发布的历史记录,因为有人可能以此为基础进行工作。如果您重写(更改)历史记录,则在合并更改和更新它们时会遇到问题。
所以解决方案是创建一个新提交 which 恢复更改你想要摆脱的。您可以使用以下方法执行此操作git 恢复命令。
您有以下情况:
A <-- B <-- C <-- D <-- master <-- HEAD
(这里的箭头指的是指针的方向:在提交的情况下是“父”引用,在分支头(branch ref)的情况下是顶部提交,在 HEAD 引用的情况下是分支的名称)。
您需要创建的内容如下:
A <-- B <-- C <-- D <-- [(BCD)-1] <-- master <-- HEAD
where [(BCD)^-1]
means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)-1 = D-1 C-1 B-1, so you can get the required situation using the following commands:
$ git revert --no-commit D
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m "the commit message for all of them"
适用于除合并提交之外的所有内容。
替代解决方案是checkout contents提交 A,并提交此状态。也适用于合并提交。但是,添加的文件不会被删除。如果您有任何本地更改git stash
他们首先:
$ git checkout -f A -- . # checkout that revision over the top of local files
$ git commit -a
那么你就会出现下面的情况:
A <-- B <-- C <-- D <-- A' <-- master <-- HEAD
提交 A' 与提交 A 具有相同的内容,但是不同的提交(提交消息、父级、提交日期)。
备用Jeff Ferland 的解决方案,Charles Bailey 修改建立在相同的想法之上,但使用git重置。这里稍微修改一下,这种方式适用于一切:
$ git reset --hard A
$ git reset --soft D # (or ORIG_HEAD or @{1} [previous location of HEAD]), all of which are D
$ git commit