代码之家  ›  专栏  ›  技术社区  ›  Tanmay

新提交被拒绝,因为我删除了上次提交

git
  •  3
  • Tanmay  · 技术社区  · 7 年前

    git reset --hard HEAD^
    

    头部现在在b760747随机提交

    然后我做了一些更改并尝试添加+提交+推送它。但失败,错误消息如下:

    无法将某些引用推送到(存储库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 回复  |  直到 7 年前
        1
  •  2
  •   Arghya Saha    7 年前

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

    git push -f origin branch_name
    

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

        2
  •  0
  •   Romain Valeri    7 年前

    我想添加强制推送方法的替代方法,在某些情况下,强制推送方法可能是一个很大的no no(即, 如果你不是唯一一个参与回购的人,那么人们可以(也会!)有历史冲突

    这不是什么大秘密,但让我们把它加上:

    # 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
    

    我承认它看起来有点笨重,给历史增添了噪音,但你没有重写历史,你的共同用户会更少地恨你。万岁!