如何强制将重置推送到远程存储库?

sam*_*ell 91 git

我们的远程主分支不知怎的搞砸了.当前开发代码与最新提交一起在主分支上.显然,开发代码还没有为主分支做好准备.

所以在我的本地存储库中,我重置了最新的标记,git reset --hard (Tag).现在主分支在我的本地存储库上是正确的.现在,当我尝试将更改推送到远程存储库时git push origin master,我收到一个错误:

To (REMOTE GIT REPOSITORY LOCATION)
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '(REMOTE GIT REPOSITORY LOCATION)'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

所以环顾四周之后,我发现了这个--force选项.所以我强行推进了远程存储库,git push --force origin master我仍然遇到错误:

Total 0 (delta 0), reused 0 (delta 0)
remote: error: denying non-fast-forward refs/heads/master (you should pull first)
To (REMOTE GIT REPOSITORY LOCATION)
 ! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to '(REMOTE GIT REPOSITORY LOCATION)'
Run Code Online (Sandbox Code Playgroud)

我不能对master进行拉动,因为它包含的开发代码不能在master上.

svi*_*ick 144

该消息表示您不允许进行非快进推送.

您的远程存储库很可能denyNonFastforwards = true在其配置中.如果你改变它,git push --force应该工作.

要更改设置,您需要使用远程存储库访问计算机.从那里,做git config receive.denynonfastforwards false.

  • 错误消息将有一行以"错误:无法将某些引用推送到<您的存储库>"开头,其中<您的存储库>是以.git结尾的路径,该目录包含名为"config"的文件.您可以在此"config"文件中设置denyNonFastforwards = false (4认同)
  • 你能为服务器做一个“git config”吗?或者也许你是用这个来比喻的。为了发挥这些想法,我在“/opt/git”(我的 git 服务器空间)中创建了一个测试存储库,然后在“/opt/git/the_repo/the_repo.git/config”中修改了此设置。但是一旦完成,`git push --force origin SHA:branch`就按要求工作了。 (2认同)
  • 这最初令人沮丧,但它的美妙之处在于:默认情况下远程是完全受到保护的,如果您作为开发人员有意且正确地进行变基,则可以覆盖此配置以允许危险行为。变基是每个 git 用户都应该知道如何做的事情,并且知道何时不应该做。[doc1](https://git-scm.com/book/id/v2/Git-Branching-Rebasing) [doc2](https://www.atlassian.com/git/tutorials/merging-vs-rebasing) (2认同)

ric*_*cho 15

遥控器不允许非快进.

您最好的选择是git revert所有不应该存在的提交,并且将来会更加小心.

git revert [commit]将创建一个新的提交,撤消[commit]所做的任何事情.


eme*_*ery 12

永久启用强制推送的步骤,采用以下样式

git push -f myrepo my-branch
Run Code Online (Sandbox Code Playgroud)

在远程存储库上以".git"结尾的文件夹中编辑名为"config"的文件

在git的失败推送的命令行输出中,查找如下所示的行:

error: failed to push some refs to 'ssh://user@some-remote-server.mycompany.com/srv/git/myrepo.git
Run Code Online (Sandbox Code Playgroud)

然后

ssh user@some-remote-server.mycompany.com
cd /srv/git/myrepo.git
vi config
Run Code Online (Sandbox Code Playgroud)

将"denyNonFastforwards"设置为false

在"配置"中,设置

[receive]
        denyNonFastforwards = false
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用-f从本地计算机进行推送

git push -f myrepo my-branch
Run Code Online (Sandbox Code Playgroud)


Chr*_*det 11

尝试使用该-f标志并将其放在远程分支名称之后.

git push origin master -f

  • 不,那也不起作用。我也试过 `git push -f origin master` 和相同的结果。两次我都尝试过,我得到了错误消息的第二个版本。 (2认同)