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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10544139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:40:14  来源:igfitidea点击:

How to force push a reset to remote repository?

git

提问by samwell

Our remote master branch somehow got messed up. Current development code is on the master branch along with the latest commits. Obviously, the development code is not ready for the master branch.

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

So on my local repository, I did a reset to the latest tag, git reset --hard (Tag). The master branch is now correct on my local repository. Now when I try to push the changes on to the remote repository, git push origin master, I get an error:

所以在我的本地存储库中,我重置了最新的标签git reset --hard (Tag). master 分支现在在我的本地存储库中是正确的。现在,当我尝试将更改推送到远程存储库时,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.

So after looking around I found out the --forceoption. So I did a force push on to the remote repository, git push --force origin master, and I still got an error:

所以环顾四周后,我发现了这个--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)'

I can't do a pull on master, because it contains development code which can't be on master.

我不能对 master 进行 pull,因为它包含无法在 master 上的开发代码。

回答by svick

The message means that you're not allowed to do non-fast-forward push.

该消息意味着您不得进行非快进推送。

Your remote repository has most likely denyNonFastforwards = truein its config. If you change that, git push --forceshould work.

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

To change the setting, you need access to the machine with the remote repository. From there, do git config receive.denynonfastforwards false.

要更改设置,您需要访问具有远程存储库的机器。从那里,做git config receive.denynonfastforwards false

回答by richo

The remote doesn't allow non-fast-forwards.

遥控器不允许非快进。

Your best option is to git revertall of the commits that shouldn't be there and be more careful in future.

你最好的选择是git revert所有不应该存在的提交,并在未来更加小心。

git revert [commit]will create a new commit that undoes whatever [commit]did.

git revert [commit]将创建一个新的提交来撤消[commit]所做的一切。

回答by emery

Steps to permanently enable force push in the following style

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

git push -f myrepo my-branch

Edit the file named "config" in the folder ending in ".git" on your remote repository

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

In git's command-line output from the failed push, look for the line that says something like:

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

error: failed to push some refs to 'ssh://[email protected]/srv/git/myrepo.git

then

然后

ssh [email protected]
cd /srv/git/myrepo.git
vi config

Set "denyNonFastforwards" to false

将“denyNonFastforwards”设置为false

In "config", set

在“配置”中,设置

[receive]
        denyNonFastforwards = false

Now you can push from your local machine with -f

现在您可以使用 -f 从本地机器推送

git push -f myrepo my-branch

回答by Chris Ledet

Try using the -fflag and putting it after the remote branch name.

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

git push origin master -f

git push origin master -f

回答by filiph

You're not allowed to do git push that is not fast-forward.

你不能做不是快进的 git push 。

  1. If the remote is GitHub, go to https://github.com/$USER/$REPO/settings/branchesand un-protect the branch in question.

    enter image description here

    You have to be admin of the repo to do that.

  2. If the remote is your own git server, run git config receive.denynonfastforwards falsethere.

  1. 如果远程是 GitHub,请转到https://github.com/$USER/$REPO/settings/branches并取消保护相关分支。

    在此处输入图片说明

    你必须是 repo 的管理员才能做到这一点。

  2. 如果远程是您自己的 git 服务器,请在git config receive.denynonfastforwards false那里运行。

回答by Danilo Souza Mor?es

The best way around this is to delete the remote branch and re-send it:

解决此问题的最佳方法是删除远程分支并重新发送:

git push origin master --delete
git push origin master

回答by jglathe

For me, @svick 's hint pointed in the right direction. Since the git server I wanted to modify is actually my box, I logged into it and did a git config --global receive.denynonfastforwards falseto change all repos to accept a forced non-ff push. Didn't work out of the box. What I found was that in the config there already was receive.denynonfastforwards=trueset, and it couldn't be erased with git config --global --unset receive.denynonfastforwards. Doing the edit in the repo manually (vi config) worked, though.

对我来说,@svick 的提示指向了正确的方向。由于我想要修改的 git 服务器实际上是我的盒子,我登录它并做了一个git config --global receive.denynonfastforwards false更改所有 repos 以接受强制非 ff 推送。没有开箱即用。我发现在配置中已经receive.denynonfastforwards=true设置了,并且不能用git config --global --unset receive.denynonfastforwards. vi config不过,手动 ( ) 在repo 中进行编辑是有效的。

回答by dasra khadka

I solved by removing the master branch from protected and also default which is just over proted branch rules in setting of a repository.

我通过从 protected 和 default 中删除 master 分支来解决,这只是在存储库设置中保护分支规则。

回答by Khaled AbuShqear

I'm using this group of commands to reset my remote repo, this will re-initialize your local repo and relink with your remote repo then force push the updates.

我正在使用这组命令来重置我的远程仓库,这将重新初始化您的本地仓库并与您的远程仓库重新链接,然后强制推送更新。

I think this way won't work in your case, but may be useful for someone else

我认为这种方式不适用于您的情况,但可能对其他人有用

go to the source folder then run the commands : note that https://github.com/*.gitis your remote repo link

转到源文件夹,然后运行命令:注意这https://github.com/*.git是您的远程仓库链接

git init
git remote add origin https://github.com/*.git
git add .
git commit -m "initial commit"
git push origin master -f
git push --set-upstream origin master

**Note: this will clear all your git history on your master branch**

**Note: this will clear all your git history on your master branch**

回答by Sudheesh.M.S

The problem occurs as the current branch isn't configured properly for the PULL. First check whether the upstream branch is properly configured for the pull using - git remote show origin. You can find it under the section - Local branches configured for 'git pull':. If not, configure it using:

问题发生是因为当前分支没有为PULL正确配置。首先使用 - 检查上游分支是否为拉取正确配置git remote show origin。您可以在 -为 'git pull' 配置的本地分支部分下找到它。如果没有,请使用以下命令进行配置:

git config branch.MYBRANCH.merge refs/heads/MYBRANCH

Give appropriate branch name for the place holder - MYBRANCH

为占位符提供适当的分支名称 - MYBRANCH