【问题标题】:How to remove a previous commit in git from repository?如何从存储库中删除 git 中的先前提交?
【发布时间】:2017-07-27 05:23:54
【问题描述】:

git 在 repo 中保留以前的提交。我想知道如何进一步从我的回购历史中删除特定的提交(我有它的 ID)?

【问题讨论】:

标签: git


【解决方案1】:

正如https://stackoverflow.com/a/42522493/1507546 解释的那样,您可以使用git rebase

 # 06c8c77^ means the commit just before 06c8c77
 git rebase -i 06c8c77^

 # Your default editor configured for git will be opened with line similar to the following

在第一部分你会看到很多提交,你想要删除的在底部,你可以比较提交哈希。

pick 91c0bd0 track one
....
pick 06c8c77 bla bla bla # the one to remove

要删除它,如第二部分所述,您需要做的就是从编辑器中删除该行。

# ....
#
# If you remove a line here THAT COMMIT WILL BE LOST.

之后,您应该保存并退出您的编辑器。如果这没有错误,您需要更新远程分支。例如,如果您的分支名称是feature/my_branch,您需要执行以下操作:

git push -f origin feature/my_branch # -f or --force to replace the remote feature/my_branch with the local one

注意只要在使用rebase 时分支仅由单个开发人员使用,这是安全的。

【讨论】:

    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 2012-11-28
    • 2015-11-05
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 2018-02-10
    相关资源
    最近更新 更多