微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我不能在 github 中推送恢复的提交?

如何解决为什么我不能在 github 中推送恢复的提交?

我对 git 还很陌生。我正在做一个项目,但我使用的新提交毁了我的项目。

所以我使用 git log 和 git check out 返回到上次提交。这在我的电脑上是成功的,但是当我尝试将它推送到 github 存储库时,我看到了这个错误

error: Failed to push some refs to 'https://github.com/bamdadghoori/courses.git'

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g.,'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

bamdad@DESKTOP-MJ4O8KO MINGW64 /e/courses (master)

这些是我尝试过的一些方法

1-重新初始化并使用新提交

2-使用 git revert

解决方法

如果这确实是您想要做的,那么 git push --force 应该可以解决问题。请注意,如果您滥用它,它可能会删除您的旧作品。

就像您建议的那样,git revert 可能是更好的选择,因为它不会删除或覆盖任何历史记录。
假设您的分支 git log 如下所示:

commit 95c374fca46bee52b9fd18e090e6afdfbe73813d (HEAD -> main,origin/main)
Author: Git User <git.user@mail.com>
Date:   Fri Mar 19 17:02:49 2021 +0100

    Latest commit message

commit 1177cab6490264e922075f28804404f4e1c08f76 (origin/main,main)
Author: Git User <git.user@mail.com>
Date:   Wed Mar 17 12:35:33 2021 +0100

    Commit message of the one you would like to keep

如果你想恢复最新的提交,你可以只做 git revert HEAD (HEAD 总是指你的分支的最后一次提交)或者恢复任何提交,你可以复制粘贴提交哈希。在这种情况下git revert 95c374fca46bee52b9fd18e090e6afdfbe73813d
然后通过输入 ZZ(注意大写)或 :wp 接受可能在 vim 中打开的建议提交消息,然后按 Enter。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。