自从我删除了上一个提交

时间:2018-08-19 10:24:44

标签: git

我进行了意外的提交并推送到远程。但是后来我想撤消它。所以我执行了:

git reset --hard HEAD^
  

HEAD现在处于b760747随机提交状态

然后,我进行了一些更改,并尝试添加,提交和推送。但是它失败,并显示以下错误消息:

  

! [拒绝] postacl-> postacl(非快进)错误:   无法将一些引用推送到(repository-url)

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.

如何使一切恢复正常?

2 个答案:

答案 0 :(得分:2)

您需要强制推送到远程服务器。这样可以解决您的问题。您可以通过以下命令进行操作。

git push -f origin branch_name

但是要小心,因为这会覆盖git历史记录。您可以在this article

中了解有关问题的信息

答案 1 :(得分:0)

(免责声明:如果您独自一人在回购中,请使用argo的快速有效的方法!)

我想为强制推送方法添加替代方法,在某些情况下,它可以是一个很大的禁忌(即,如果您不孤单,那么人们可以(并且会!)与您在存储库上重写过的母版有历史冲突

这不是什么大秘密,但让我们添加它来完成:

# make sure you're on the right branch (here I assume it's "master")
git checkout master
# let's get upo-to-date with the (now wrong) remote master
git pull
# alternatively to this pull, you could have undone your reset with reflog
# be free to use the one you're more familiar with

# locate the "bad" commit and store its hash for further use
# (I'll do a log but you could have prefered methods.)
git log --oneline

# create a commit which has the exact opposite modifications of <bad> commit.
git revert <bad>
# (no need to commit the previous action since the commit is already created)

# push the new commit on top of remote master (I assume "origin" for your remote)
git push origin master

我会让您觉得它有些笨拙,并给历史添加了噪音,但是您没有重写历史,因此您的共同用户会更讨厌您。哇!