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

为什么我不能在github中推送恢复的提交?,git,github,git-push,git-checkout,git-revert,Git,Github,Git Push,Git Checkout,Git Revert,我对git很陌生。我在做一个项目,我用了新的commit毁了我的项目 因此,我使用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

我对git很陌生。我在做一个项目,我用了新的commit毁了我的项目

因此,我使用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
commit 95c374fca46bee52b9fd18e090e6afdfbe73813d(HEAD->main,origin/main)
作者:Git用户
日期:3月19日星期五17:02:49 2021+0100
最新提交消息
提交1177CAB6490264E922075F2880404F4E1C08F76(源/主,主)
作者:Git用户
日期:3月17日星期三12:35:33 2021+0100
提交您希望保留的邮件
如果要还原最新的提交,只需执行git revert HEAD
(HEAD总是指分支的最后一次提交),或者每复制粘贴提交哈希即可还原任何提交。在这种情况下,
git revert 95c374fca46bee52b9fd18e090e6afdfbe73813d


然后通过键入
ZZ
(注意大写)或
:wp
,接受建议的提交消息,然后按Enter.

tnx。但我尝试了这种方法(git revert)。它在本地机器上很有用。但我无法在github中推送它,而且我还担心使用--force或--f推送。因为它会导致一些问题(例如,正如您所说的擦除工作或历史记录),请事先尝试
git pull
ing您可能仍然错过了您尝试还原的一次提交tnx@Wischm0pp它现在可以正常工作。当我尝试推送时,git bash中没有错误,并且我可以看到最后一次提交(我还原的)但如果我在打开存储库(默认情况下)时看到这个版本而不是错误的提交,那就更好了。