我刚开始使用 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/