313

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.

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

the
  • 21,007
  • 11
  • 68
  • 101
Asantoya17
  • 4,465
  • 6
  • 23
  • 29
  • 9
    If you are sure your local master has the latest changes you want on remote, then do git push --force – IgorGanapolsky Apr 22 '13 at 18:38
  • 72
    `git push --force ` worked for me. – Sorter Jan 05 '14 at 08:53
  • 1
    @Sorter - I've been having the same problem all day, this helped me!! Thank you. But it doesn't provide an explanation as to why the issue happened in the first place and why the fix worked. – Padawan Jul 23 '15 at 00:54
  • 2
    @pmr, just flippantly asking if the OP "read the error message" doesn't help at all. Obviously, the error message didn't make any sense to the OP, just as it didn't make any message to me, or the other 66 people who upvoted his post. Snarky, wittier-than-though don't help people at all. – Padawan Jul 23 '15 at 00:55
  • 3
    @Padawan You will also notice that this "snarky, wittier-than-though person" provided the accepted answer for this question and 72 people (probably including you) found it helpful. I'm sorry that my comment came across as arrogant, but it would have been my first question if you had asked me that question in a professional context as well. Sometimes stopping and taking a minute to carefully read the error message sometimes does wonders. I did for me when I saw that error for the first time. – pmr Jul 23 '15 at 09:35
  • @pmr Yes, I noticed that. Notice I wasn't referring to your answer. I was referring to your comment above, "Did you try to read the error message?" Notice that this provides no value. This is stereotypical on SO, and [my perspective is not unique](https://www.reddit.com/r/programming/comments/3cafkp/is_stack_overflow_overrun_by_trolls/) If you really were helping you would have followed your question with help (keyword there) explaining the error message. Note, error messages are neither intuitive nor user-friendly, just like git and github. – Padawan Jul 23 '15 at 16:44
  • I had the same problem. I think it was the same file I edited and commited twice (for Versioning). So two unpushed commits had the same file with a simple version-change. - Hope it could help someone. – MonTea Mar 27 '18 at 09:31
  • It was not this issue... i testet it on a test-repo, and i could add the same file with different changes twice before pushing... push works correct – MonTea Mar 27 '18 at 09:39
  • I think its probably happening because your remote has some changes which are not synced with the local so `git pull --rebase` and then `git push origin master` this worked for me hope this helps someone – Mahesh Jamdade Mar 06 '19 at 03:52
  • just force by command `git push origin master --force` – Omar bakhsh Feb 14 '21 at 08:50

25 Answers25

280

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

shakhmatov
  • 3,073
  • 1
  • 13
  • 12
  • 56
    No, don't try this. From ```git help push```: "This can cause the remote repository to lose commits; use it with care." – jcsahnwaldt Reinstate Monica Sep 03 '13 at 11:39
  • 8
    definitely don't do this – Lluís Sep 27 '13 at 12:58
  • 19
    this worked for me. I figured it was OK because my only commit on github was the one created with a new repo. Probably a good idea to not use it after the start of a project. – Matthew Moore Oct 05 '13 at 16:03
  • 2
    can be useful if you want to update a repo which was created for delivery purpose (so no new changes on remote side) – laplasz Oct 17 '13 at 15:24
  • I just used this as I just made a commit, synced with GitHub, then wished I had added another file to that commit. So I undid it locally, executed the above command to clear it from GitHub, then committed again locally, then synced. All good now! Mind you, I knew for a fact that no other commits were made to that branch. – zeMinimalist Oct 27 '17 at 22:36
  • By cherry picking remote commit, this works without losing remote repo commits . – Sk Saad Al Mahmud Apr 18 '18 at 04:56
  • if you work in a team, definately dont do this, I had done it, good to me that I didnt screw up the repository – Marcelo May 29 '18 at 19:49
  • This worked for me. But in my case it was a fresh repository that I mistakenly cloned the wrong project file to so make sure you know what you are doing before you use this command to avoid any damages. – John Jul 31 '20 at 09:27
  • even though it deleted all my previous commits, it's amazing – necip Jul 13 '21 at 14:21
215

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.

pmr
  • 58,701
  • 10
  • 113
  • 156
  • 1
    it shows me this Auto-merging README.md Automatic merge failed; fix conflicts and then commit the result. – Asantoya17 Jul 27 '12 at 22:30
  • 1
    @Asantoya17 Changes that have been done on the remote master conflict with your changes. Review the file and review them. Then commit the results of resolving. Please get a tutorial or book or at least read the messages. – pmr Jul 27 '12 at 22:35
  • thanks for your help I follow yours intructions and solved it – Asantoya17 Jul 27 '12 at 23:00
  • 1
    What does pull means? Enables you to push a file or fetching and downloading a file from the repository's website? (e.g. Bit Bucket) – David Dimalanta May 03 '13 at 09:44
  • @David why would "pull" mean "push"? – pmr May 03 '13 at 10:45
  • @pmr Yes I see now that my comments concerned a slightly different problem. I have deleted my comment to avoid further confusion. – Finn Årup Nielsen Apr 14 '15 at 13:21
  • So what happens when you really do not care that you are out of sync with the master and you want to push your code upstream anyway without pulling down the failed attempts of others. And... can I do this with the Eclipse plug in. – Andrew S Aug 12 '15 at 23:03
  • 7
    @AndrewS `git push --force origin master`. If you run into those kind of issues more than once in your life, your project workflow is broken. Features should be developed in branches and merged without fast-forwarding and if a feature has "failed" you should revert the merge commit (preferably do your testing in an integration branch first before you merge a feature to master). No idea about Eclipse though. – pmr Aug 13 '15 at 01:06
  • 2
    What to do if that error reports by `git pull origin trunk:master`? – Andry Jul 28 '18 at 20:21
16

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
blue-sky
  • 51,962
  • 152
  • 427
  • 752
15

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.

Umar Farooq
  • 151
  • 1
  • 2
12

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.

Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
10

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
Muneef M
  • 1,094
  • 15
  • 17
  • This answer is already given: http://stackoverflow.com/a/16374574/1771479, and not advised to do. – agold Dec 07 '15 at 10:18
10

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
Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
9

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.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
8

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"
Guim
  • 628
  • 2
  • 12
  • 26
6

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

firefly28
  • 119
  • 1
  • 4
4

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.

cage
  • 103
  • 1
  • 8
4

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

4

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
3

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.

the
  • 21,007
  • 11
  • 68
  • 101
2

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

m4n0
  • 29,823
  • 27
  • 76
  • 89
Muhammad Numan
  • 237
  • 4
  • 20
1

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

Son Nguyen Thanh
  • 1,199
  • 15
  • 19
1

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.

1

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*
Harshal SG
  • 403
  • 3
  • 7
1

I solved this problem with these steps:

git add *
git fetch
git pull origin main --allow-unrelated-histories or git pull --rebase <your_reponame> <your_branch>
git push -u "<pass your project clone url>" main or git pull origin [branch]
Koedlt
  • 4,286
  • 8
  • 15
  • 33
M. Kutlu SENGUL
  • 101
  • 1
  • 2
0

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.

  • I always keep a backup of my git repo on Dropbox. That way I don't have to delete the local repo, I just copy and paste relevant directories from Dropbox when the situation arises. – IgorGanapolsky Apr 22 '13 at 18:36
0

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

0

Try this, in my case it works

git rebase --abort

Ahmer Afzal
  • 501
  • 2
  • 11
  • 24
0

This means that your remote git repository is configured with denyNonFastforwards = true. Although dangerous, sometimes you might really need to overwrite the git history (e.g. after running bfg --strip-blobs-bigger-than 100M) thus you can temporarily modify that setting to false (denyNonFastforwards = true, see it in your-repository.git/config) then forcibly push (i.e. git push -f).

Adrian
  • 3,321
  • 2
  • 29
  • 46
0

Sometimes, Git can't make your change to a remote repository without losing commits.In simple words,this may happen because your commit was lost or if someone else is trying to push to the same branch as you. So to fix this, first make a pull request

$ git pull origin YOUR_BRANCH_NAME

Or

$ git pull --rebase origin master
Manishyadav
  • 1,361
  • 1
  • 8
  • 26
-1

for me git push -f origin master worked

Helmar Bachle
  • 49
  • 1
  • 1
  • 9