完全删除较早的提交
Completely remove an earlier commit
考虑以下提交历史:
* 627f412 (HEAD -> master) final commit
* 1425cf8 added some file
* 2c8bb78 initial commit
在这些提交期间既不推送也不拉取存储库。
第二次提交 1425cf8 是不必要的提交。我想完全删除该提交。
因此,新日志将如下所示:
* 627f412 (HEAD -> master) final commit
* 2c8bb78 initial commit
我该如何实现?
在这种情况下 git reset --hard HEAD~1
是正确的做法吗?
Is git reset --hard HEAD~1 the correct thing to do in this situation?
否:git rebase -i 2c8bb78
是。
您可以在交互式变基会话中删除额外的提交。 (如果你想完全放弃 1425cf8 介绍的内容)
或者你可以压缩这两个提交。
为此,在交互式会话中(参见“Git Tools - Rewriting History”):
- 在 627f412 前面加一个
s
for squash
请注意,最终结果将 而不是 627f412:新提交将具有不同的 SHA1,因为其内容会有所不同。
您可以(无需 --hard
!)将您的 HEAD 重置为 2c8bb78
,然后提交更改。
或者在 2c8bb78 上变基并压缩到另外两个提交(627f412
和 1425cf8
)。
使用 reset --hard
,您可能会丢失修改。
考虑以下提交历史:
* 627f412 (HEAD -> master) final commit
* 1425cf8 added some file
* 2c8bb78 initial commit
在这些提交期间既不推送也不拉取存储库。
第二次提交 1425cf8 是不必要的提交。我想完全删除该提交。
因此,新日志将如下所示:
* 627f412 (HEAD -> master) final commit
* 2c8bb78 initial commit
我该如何实现?
在这种情况下 git reset --hard HEAD~1
是正确的做法吗?
Is git reset --hard HEAD~1 the correct thing to do in this situation?
否:git rebase -i 2c8bb78
是。
您可以在交互式变基会话中删除额外的提交。 (如果你想完全放弃 1425cf8 介绍的内容)
或者你可以压缩这两个提交。
为此,在交互式会话中(参见“Git Tools - Rewriting History”):
- 在 627f412 前面加一个
s
for squash
请注意,最终结果将 而不是 627f412:新提交将具有不同的 SHA1,因为其内容会有所不同。
您可以(无需 --hard
!)将您的 HEAD 重置为 2c8bb78
,然后提交更改。
或者在 2c8bb78 上变基并压缩到另外两个提交(627f412
和 1425cf8
)。
使用 reset --hard
,您可能会丢失修改。