GIT 无法恢复到之前的提交 - 远程不接受推送

标签 git git-commit atlassian-sourcetree

我刚开始使用 Git,我有一些东西需要恢复到之前的提交。我可以在本地执行此操作,没问题。

git reset --hard hashcode(或使用sourcetree)

但是,当我尝试推送到 bitbucket 时,souretree 会给我一个错误,直到我 pull 下之前的提交,然后我又回到了我开始无法恢复到之前的提交的地方。l

我如何在本地恢复到之前的提交,然后推送到 bitbucket 覆盖并删除我要恢复到的提交之后的所有提交?

错误信息是:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin Development:Development
Pushing to git@bitbucket.org:Username/project.git

To git@bitbucket.org:Username/project.git
 ! [rejected]        Development -> Development (non-fast-forward)
error: failed to push some refs to 'git@bitbucket.org:Username/project.git'

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

最佳答案

你可以简单地强制推送:

git push -f bitbucket

但这假设没有人已经从您的 BitBucket 存储库中提取数据(否则他们将不得不将其本地分支重置为新的远程历史记录)。
另外,一旦强制推送,就会有 no way to know who changed that history on the remote side .

这与你的error message是一致的:

To git@bitbucket.org:Username/project.git
 ! [rejected]        Development -> Development (non-fast-forward)
error: failed to push some refs to 'git@bitbucket.org:Username/project.git'

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这是“git push Note about fast-forwards”的摘录:

In such a case, and only if you are certain that nobody in the meantime fetched your earlier commit A (and started building on top of it), you can run "git push --force" to overwrite it.
In other words, "git push --force" is a method reserved for a case where you do mean to lose history.


另一个(最安全的)选项是使用 git revert (甚至 for a range of commits ),创建一个新的提交(取消旧的),您可以推送。

关于GIT 无法恢复到之前的提交 - 远程不接受推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22576133/

相关文章:

java - 将 "git describe"输出传递给 Java 代码

GIT:我是否需要在 checkout 另一个分支之前提交我的分支,那么存储呢?

git - 如何禁用 git gpg 签名

git - 删除本地开发分支

git - Hook 还是不 Hook - git

git - 无法使用子模块推送 Git 存储库的提交

atlassian-sourcetree - 如何在Mac上注册Source Tree?

git - 解决 git 冲突 : git mergetool doesn't work

git - 如何以编程方式找到两个分支的共同祖先

git - 两个人的提交可以有相同的哈希值吗?