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

GitPush

Git Problem Overview


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 '[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.

Git Solutions


Solution 1 - Git

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.

⚠️ Try this: git push -f origin master

Solution 2 - Git

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.

Solution 3 - Git

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 :

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

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

WARNING:

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

Solution 8 - Git

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"

Solution 9 - Git

My Remote was not in sync with the local so this worked for me

git pull --rebase

and make sure when you do git pull again it should say Already up to date and now you are ready to push to origin

assuming you have already git remote add origin remote repository URL

do

git push origin master

The Screenshot says it all enter image description here

Alternatively you can do this

  1. git stash (stores uncommited work temporarily)
  2. git pull (make your local and remote in sync)
  3. git stash pop (get back you uncommited changes )
  4. git push

Solution 10 - Git

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

Solution 11 - Git

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.

Solution 12 - Git

! [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

Solution 13 - Git

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

Solution 14 - Git

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.

Solution 15 - Git

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

Solution 16 - Git

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.

Solution 17 - Git

This may also be caused due to some name error caused while giving name to the Repo. If any of the above answers haven't worked .This worked for me:

Delete that repo and create a new one and try the following commands again:

cd 'Local Directory Path'
git remote add origin *your_git_name.git*
git push -u origin master

if add origin shows already exists use this instead:

git remote set-url origin *your_git_name.git*

Solution 18 - Git

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.

Solution 19 - Git

If anyone has this error while trying to push to heroku then just replace 'origin' with 'heroku' like this: git push -f heroku master

Solution 20 - Git

Try this, in my case it works

> git rebase --abort

Solution 21 - Git

I also got the same error but I solved it easily by fetching its URLs as

git fetch origin Your repository origin

then simply applying the command

git push -f origin master

You will get a good result

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAsantoya17View Question on Stackoverflow
Solution 1 - GitshakhmatovView Answer on Stackoverflow
Solution 2 - GitpmrView Answer on Stackoverflow
Solution 3 - Gitblue-skyView Answer on Stackoverflow
Solution 4 - GitAlexander VolkovView Answer on Stackoverflow
Solution 5 - GitUmar FarooqView Answer on Stackoverflow
Solution 6 - GitMuneef MView Answer on Stackoverflow
Solution 7 - GitGuptaView Answer on Stackoverflow
Solution 8 - GitgtechView Answer on Stackoverflow
Solution 9 - GitMahesh JamdadeView Answer on Stackoverflow
Solution 10 - Gitfirefly28View Answer on Stackoverflow
Solution 11 - GitcageView Answer on Stackoverflow
Solution 12 - Gituser3292712View Answer on Stackoverflow
Solution 13 - GitJonn Marc VistalView Answer on Stackoverflow
Solution 14 - GitkqwView Answer on Stackoverflow
Solution 15 - GitSon Nguyen ThanhView Answer on Stackoverflow
Solution 16 - GitPaulo Linhares - PackappsView Answer on Stackoverflow
Solution 17 - GitHarshal SGView Answer on Stackoverflow
Solution 18 - GitAkash Kumar SharmaView Answer on Stackoverflow
Solution 19 - GitVadim MalakhovskiView Answer on Stackoverflow
Solution 20 - GitAhmer AfzalView Answer on Stackoverflow
Solution 21 - GitMuhammad NumanView Answer on Stackoverflow