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

时间:2014-03-22 10:05:01

标签: git git-commit atlassian-sourcetree

我刚刚开始使用Git而且我已经得到了一些我需要转换到之前提交的东西。我可以在本地做这个没问题。

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

然而,当我尝试推送到bitbucket时,souretree给了我一个错误,直到我将之前的提交拉下来并且我回到了我开始无法恢复到之前的commit.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.

1 个答案:

答案 0 :(得分:6)

你可以强行推动:

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"

的摘录
  

在这种情况下,并且只有在您确定没有人同时获取您之前的提交A(并开始在其上构建)时,您才能运行" git push --force "覆盖它。
  换句话说," git push --force"是一种保留用于意图丢失历史记录的方法。


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