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

2019-01-12 14:37发布

问题:

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 master I get this error:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com: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.

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

回答1:

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

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



回答2:

Try this: git push -f origin master



回答3:

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 pull https://github.com/*username*/*repository*.git master

repository URL is got from here on project github page :

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

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

Then push :

git push


回答4:

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.

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

git push origin master --force


回答5:

If git pull does not help, then probably you have pushed your changes (A) and after that had used git commit --amend to 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.

             B
            /
        ---X---A

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

However, if there are changes after A from other person:

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

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

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

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



回答6:

WARNING:

Going for a 'git pull' is not ALWAYS a solution. 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 pull will undo all of the changes you made to your history, intentionally.



回答7:

! [rejected] master -> master (non-fast-forward)

Don’t panic, this is extremely easy to fix. All you have to do is issue a pull and your branch will be fast-forward:

$ git pull myrepo master

Then retry your push and everything should be fine:

$ git push github master



回答8:

Try this command: "git pull origin master"

It worked for me.

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



回答9:

use this command:

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

like:

git pull --allow-unrelated-histories origin master

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



回答10:

this command worked well for me

git push -f origin master


回答11:

You need to do

git branch

if the output is something like:

* (no branch)
master

then do

git checkout master

Make sure you do not have any pending commits as checking out will lose all non-committed changes.



回答12:

I had this problem on a development machine. The dev branch was pushing fine but the the master branch gave me (while git pushing when being on the dev branch):

! [rejected]        master -> master (non-fast-forward)

So I tried:

git checkout master
git pull

Which gave me:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either.

I found out the master branch was missing from .git/config and added:

[branch "master"]
    remote = origin
    merge = refs/heads/master

Afterwards git push also worked fine on the dev branch.



回答13:

This happened to me when I was on develop branch and my master branch is not with latest update.

So when I tried to git push from develop branch I had that error.

I fixed it by switching to master branch, git pull, then go back to develop branch and git push.

$ git fetch && git checkout master
$ git pull
$ git fetch && git checkout develop
$ git push


回答14:

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:

git push -f origin master

Remember to first commit your changes:

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


回答15:

The only i was able to resolve this issue was to delete the local and git repo and create the same again at both ends. Works fine for now.



回答16:

I had same as issue. I use Git Totoise. Just Right Click ->TotoiseGit -> Clean Up . Now you can push to Github It worked fine with me :D



回答17:

This is because you have made conflicting changes to its master. And your repository server is not able to tell you that with these words, so it gives this error because it is not a matter of him deal with these conflicts for you, so he asks you to do it by itself. As ?

1- git pull This will merge your code from your repository to your code of your site master. So conflicts are shown.

2- treat these manualemente conflicts.

3-

git push origin master

And presto, your problem has been resolved.



标签: git push