git - 获取错误 "Updates were rejected because the tip of your current branch is behind"

标签 git github bitbucket

我的本​​地系统代码已与 Bitbucket 的远程系统同步,但出现了一些问题,因此我通过运行 git reset --hard HEAD^ 删除了最后一次提交。之后我做了一些更改并提交了这些更改。现在,当我尝试将这些更改推送到远程时,我收到以下消息:

[vagrant@localhost horizon]$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Password for 'https://user_name@bitbucket.org': 
To https://user_name@bitbucket.org/user_name/repo_name.git
 ! [rejected]        stable/kilo -> stable/kilo (non-fast-forward)
error: failed to push some refs to 'https://user_name@bitbucket.org/user_name/repo_name.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.   

我的问题是,在这种情况下我该如何推送?请解释。

最佳答案

您执行了以下命令:

 git reset --hard HEAD^

结果是历史重写。想象一下本地和远程的以下历史记录:

本地

A->B->C->D

远程

A->B->C->D

现在

git reset --hard HEAD^

一些哈希将被重写例如:

本地

A1->B1->C1->D1

远程

A->B->C->D

因此您的历史与远程历史不同。这意味着对于 git,你需要 merge 。 那么,您的下一个问题将是:如何才能摆脱这种情况? 很高兴你问:

<强>1。注意:永远不要重置历史并尝试推送,尤其是当你推送 --force 时!

  • 重置历史很好,如果你知道你在做什么。

一个可能的解决方案是还原您首先重置的提交并推送此还原的提交。但是为此你需要取回你用 git reset 重置的“糟糕的提交”。 您可以通过 git reflog 执行此操作并从那里提取提交 SHA-1 并重置为此提交。然后你就处于提交之前的状态。 之后,你执行 git reset HEAD^ 就安全了!

关于git - 获取错误 "Updates were rejected because the tip of your current branch is behind",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509324/

相关文章:

ruby - github boxen 可以在 mac os 10.7 (Lion) 上运行吗

amazon-web-services - AWS 部署 "Unable to remove top level folder"

Git 警告 : "Disabling bitmap writing"

git - 恢复已添加到 git、未提交然后被 git reset --hard 意外删除的文件

Git:允许修改提交而不删除历史记录

ruby-on-rails - Travis CI为每个Github提交推送创建两个构建

git - 我如何确定 git 历史是否是线性的?

git reset --hard <sha1>.这是否删除了所有内容?

git 服务器端 'update' Hook 在 bitbucket 中

visual-studio - 用于 git 推送错误的 Microsoft Visual Studio 工具