git 拒绝主 -> 主(非快进)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11696295/
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 14:14:59  来源:igfitidea点击:

rejected master -> master (non-fast-forward)

gitpush

提问by Asantoya17

I'm trying to push my project (all files in a new repository). I follow the steps but when I push with git push -u origin masterI get this error:

我正在尝试推送我的项目(新存储库中的所有文件)。我按照步骤操作,但是当我推送时出现git push -u origin master此错误:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:asantoya/projectnewbies.git'
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.

I got this error many times and can't figure out what to do.

我多次收到此错误,不知道该怎么做。

采纳答案by pmr

As the error message says: git pullbefore you try to git push. Apparently your local branch is out of sync with your tracking branch.

正如错误消息所说:git pull在您尝试之前git push。显然,您的本地分支与您的跟踪分支不同步。

Depending on project rules and your workflow you might also want to use git pull --rebase.

根据项目规则和您的工作流程,您可能还想使用git pull --rebase.

回答by shakhmatov

NOTICE:This is never a recommended use of git. This will overwrite changes on the remote. Only do this if you know 100% that your local changes should be pushed to the remote master.

注意:这绝不是 git 的推荐用法。这将覆盖遥控器上的更改。仅当您 100% 知道您的本地更改应该推送到远程主服务器时才这样做。

Try this: git push -f origin master

尝试这个: git push -f origin master

回答by raed

this command worked well for me

这个命令对我很有效

git push -f origin master

回答by blue-sky

I've just received this error.

我刚刚收到此错误。

I created a github repository after creating my local git repository so I needed to accept the changes into local before pushing to github. In this case the only change was the readme file created as optional step when creating github repository.

我在创建本地 git 存储库后创建了一个 github 存储库,因此我需要在推送到 github 之前接受对本地的更改。在这种情况下,唯一的变化是创建 github 存储库时作为可选步骤创建的自述文件。

git pull https://github.com/*username*/*repository*.git master

repository URL is got from here on project github page :

存储库 URL 来自项目 github 页面:

enter image description here

在此处输入图片说明

I then re-initialised (this may not be needed)

然后我重新初始化(这可能不需要)

git init
git add .
git commit -m "update"

Then push :

然后推:

git push

回答by Gupta

WARNING:

警告:

Going for a 'git pull' is not ALWAYSa solution, so be carefull. You may face this problem (the one that is mentioned in the Q) if you have intentionally changed your repository history. In that case, git is confusing your history changes with new changes in your remote repo. So, you should go for a git push --force, because calling git pullwill undo all of the changes you made to your history, intentionally.

寻找“ git pull”并不总是一个解决方案,所以要小心。如果您有意更改了存储库历史记录,您可能会遇到这个问题(Q 中提到的那个)。在这种情况下,git 会将您的历史更改与远程存储库中的新更改混淆。因此,您应该选择 a git push --force,因为调用git pull将撤消您有意对历史记录所做的所有更改。

回答by Muneef M

i had created new repo in github and i had the same problem, but it also had problem while pulling, so this worked for me.

我在 github 中创建了新的 repo 并且我遇到了同样的问题,但是在拉动时它也有问题,所以这对我有用。

but this is not advised in repos that already have many codes as this could mess up everything

但是在已经有很多代码的回购中不建议这样做,因为这可能会弄乱一切

git push origin master --force

回答by Alexander Volkov

If git pulldoes not help, then probably you have pushed your changes (A) and after that had used git commit --amendto add some more changes (B). Therefore, git thinks that you can lose the history - it interprets B as a different commit despite it contains all changes from A.

如果git pull没有帮助,那么您可能已经推送了您的更改(A),然后再git commit --amend添加一些更改(B)。因此,git 认为您可能会丢失历史记录 - 尽管它包含来自 A 的所有更改,但它将 B 解释为不同的提交。

             B
            /
        ---X---A

If nobody changes the repo after A, then you can do git push --force.

如果之后没有人更改 repo A,那么您可以这样做git push --force

However, if there are changes after Afrom other person:

但是,如果在A其他人之后发生了变化:

             B
            /
        ---X---A---C

then you must rebase that persons changes from Ato B(C->D).

那么您必须将人员从更改AB( C-> D) 的基础变基。

             B---D
            /
        ---X---A---C

or fix the problem manually. I didn't think how to do that yet.

或手动修复问题。我还没想好怎么做。

回答by Umar Farooq

use this command:

使用这个命令:

git pull --allow-unrelated-histories <nick name of repository> <branch name>

git pull --allow-unrelated-histories <nick name of repository> <branch name>

like:

喜欢:

git pull --allow-unrelated-histories origin master

git pull --allow-unrelated-histories origin master

this error occurs when projects don't have any common ancestor.

当项目没有任何共同的祖先时会发生此错误。

回答by gtech

This is because you did some changes in your master so the project ask you to pull first. If you want to push it anyway you can use brute force by typing this:

这是因为您在 master 中做了一些更改,因此项目要求您先拉取。如果您无论如何都想推动它,您可以通过键入以下内容使用蛮力:

git push -f origin master

Remember to first commit your changes:

请记住首先提交您的更改:

git add .
git commit -m "Your commit message"

回答by firefly28

Try this command: "git pull origin master"

试试这个命令:“git pull origin master”

It worked for me.

它对我有用。

Check this link: https://help.github.com/articles/dealing-with-non-fast-forward-errors

检查此链接:https: //help.github.com/articles/dealing-with-non-fast-forward-errors