rejected master -> master (non-fast-forward)
Asked Answered
T

27

329

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.

Twiggy answered 27/7, 2012 at 22:9 Comment(10)
If you are sure your local master has the latest changes you want on remote, then do git push --forceWicker
git push --force <remote_repository> worked for me.Trichinosis
@Trichinosis - 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.Chandachandal
@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.Chandachandal
@Chandachandal 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.Popsicle
@Popsicle 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 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.Chandachandal
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.Ultimatum
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 correctUltimatum
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 someoneCaitlin
just force by command git push origin master --forceCulex
P
221

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.

Popsicle answered 27/7, 2012 at 22:25 Comment(9)
it shows me this Auto-merging README.md Automatic merge failed; fix conflicts and then commit the result.Twiggy
@Twiggy 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.Popsicle
thanks for your help I follow yours intructions and solved itTwiggy
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)Jeb
@David why would "pull" mean "push"?Popsicle
@Popsicle Yes I see now that my comments concerned a slightly different problem. I have deleted my comment to avoid further confusion.Flashlight
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.Investment
@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.Popsicle
What to do if that error reports by git pull origin trunk:master?Jellybean
S
291

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

Silicate answered 4/5, 2013 at 12:59 Comment(9)
No, don't try this. From git help push: "This can cause the remote repository to lose commits; use it with care."Rile
definitely don't do thisTropology
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.Loft
can be useful if you want to update a repo which was created for delivery purpose (so no new changes on remote side)Crocoite
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.Silky
By cherry picking remote commit, this works without losing remote repo commits .Halsy
if you work in a team, definately dont do this, I had done it, good to me that I didnt screw up the repositoryTitle
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.Cowbane
even though it deleted all my previous commits, it's amazingBrandling
P
221

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.

Popsicle answered 27/7, 2012 at 22:25 Comment(9)
it shows me this Auto-merging README.md Automatic merge failed; fix conflicts and then commit the result.Twiggy
@Twiggy 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.Popsicle
thanks for your help I follow yours intructions and solved itTwiggy
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)Jeb
@David why would "pull" mean "push"?Popsicle
@Popsicle Yes I see now that my comments concerned a slightly different problem. I have deleted my comment to avoid further confusion.Flashlight
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.Investment
@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.Popsicle
What to do if that error reports by git pull origin trunk:master?Jellybean
G
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
Gray answered 13/12, 2013 at 23:52 Comment(1)
This answer reflects the actual case scenario.Jerkin
T
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.

Tenuis answered 16/9, 2016 at 15:12 Comment(0)
C
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.

Capua answered 30/4, 2014 at 9:5 Comment(0)
C
11

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
Caitlin answered 6/3, 2019 at 4:2 Comment(0)
L
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
Levantine answered 7/12, 2015 at 9:55 Comment(1)
This answer is already given: https://mcmap.net/q/54514/-rejected-master-gt-master-non-fast-forward, and not advised to do.Vassar
B
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.

Buckeye answered 15/9, 2016 at 12:45 Comment(0)
H
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"
Hermanhermann answered 20/3, 2018 at 16:55 Comment(0)
M
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

Menedez answered 25/3, 2014 at 15:10 Comment(0)
E
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.

Eriha answered 3/9, 2013 at 21:4 Comment(0)
I
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

Intermixture answered 10/2, 2014 at 11:52 Comment(0)
F
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
Frager answered 17/8, 2016 at 18:17 Comment(0)
D
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.

Delly answered 3/9, 2014 at 13:10 Comment(0)
C
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

Cardiogram answered 2/10, 2021 at 5:57 Comment(0)
D
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

Daze answered 22/12, 2014 at 12:39 Comment(0)
M
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.

Majka answered 4/4, 2016 at 19:51 Comment(0)
S
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*
Septuagesima answered 29/3, 2020 at 7:58 Comment(0)
M
1

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).

Microsecond answered 3/9, 2022 at 13:24 Comment(0)
C
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]
Carder answered 11/1, 2023 at 21:59 Comment(0)
S
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.

Saragossa answered 26/3, 2013 at 4:27 Comment(1)
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.Wicker
B
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

Brisbane answered 11/3, 2019 at 0:11 Comment(0)
M
0

Try this, in my case it works

git rebase --abort

Melodist answered 20/3, 2020 at 18:29 Comment(0)
I
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
Italia answered 4/10, 2022 at 17:56 Comment(0)
D
0

just use these 2 commands

git pull --rebase

git push origin

if not able to push try to force push.

Discreditable answered 2/11, 2023 at 11:58 Comment(1)
Both to rebase (or merge for that matter) and definitely to force push are very opinionated things to do and not just something that you “just use” to get rid of some error message; an answer that mentions them should explain what they do.Alver
A
-1

Try with:

git push --force urlRepository

Amenra answered 29/9, 2023 at 22:48 Comment(0)
F
-2

for me git push -f origin master worked

Fill answered 9/5, 2023 at 17:17 Comment(2)
Duplicate of https://mcmap.net/q/54514/-rejected-master-gt-master-non-fast-forwardAlver
Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful. - From ReviewOptics

© 2022 - 2024 — McMap. All rights reserved.