136

I'm getting following error, whn trying first Github push:

[rejected] master -> master (non-fast forward)
error: failed to push some refs to 'git@github.com:me/me.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.

how can I fix this and merge remote changes?

trejder
  • 17,148
  • 27
  • 124
  • 216
John
  • 5,139
  • 19
  • 57
  • 62

7 Answers7

108

See the 'non-fast forward' section of 'git push --help' for details.

You can perform "git pull", resolve potential conflicts, and "git push" the result. A "git pull" will create a merge commit C between commits A and B.

Alternatively, you can rebase your change between X and B on top of A, with "git pull --rebase", and push the result back. The rebase will create a new commit D that builds the change between X and B on top of A.

binOr
  • 2,551
  • 1
  • 22
  • 18
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 16
    I keep running into this except that if I do a "git pull" I am told "Already up-to-date." and if I do a "git pull --rebase" I'm told that "Current branch master is up to date." Any ideas? Thanks! – jwl Sep 28 '11 at 15:08
  • 3
    @larson4 I got the same issue, but after you do the `git pull`, do another commit and then it should be good – Patrick Jan 10 '12 at 15:28
  • 20
    @Patrick @larson I had a similar problem caused by the fact that I did not read the error message carefully. The reject was on a branch that I didn't have checked out. The branch I was actually on was succeeding. The solution was to `git checkout other-branch; git pull; git push; git checkout branch-i-was-working-on`. – Noah Spurrier Sep 18 '12 at 19:44
88

You can also force a push by adding the + symbol before your branch name.

git push origin +some_branch
SammyK
  • 983
  • 11
  • 20
  • 1
    Thank you, this one works for me. The other solutions would completely erase the effect of my `git reset --hard` – Oli Apr 03 '12 at 08:48
  • 1
    Thanks! I had pushed branch "A" up to my Heroku staging app to test some functionality in a production environment. Then (locally) I merged "A" and "B" into "master" and wanted to push "master" into my staging app. Was having all sorts of problems. This made pushing the "master" very simple. Thanks! – Don Leatham May 03 '12 at 04:30
  • Worked for me too. Took me 4 hours to find the issue. Thanks a lot. I run Netbeans on local machine (windows 7) and wanted on each push on local, check out on remote machine (linux). – Maxim Shoustin Nov 10 '12 at 16:09
  • Note that this method might not be safe, and it can cause some diverged commits to be unreachable. – samuil Jul 02 '13 at 08:18
  • This should be the accepted solution. The other solutions were not working for me – banarun Oct 11 '14 at 18:26
  • I agree. this is the better solution. The comment thread above about just re-committing the file, still displayed the same error. – thinker Feb 28 '20 at 14:12
20

You probably have changes on github that you never merged. Try git pull to fetch and merge the changes, then you should be able to push. Sorry if I misunderstood your question.

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
  • 18
    In case you need to reject changes in the remote master and push your own changes, try to push with -f key – Hotsyk Nov 11 '09 at 09:33
13

If you "git pull" and it says "Already up-to-date.", and still get this error, it might be because one of your other branches isn't up to date. Try switching to another branch and making sure that one is also up-to-date before trying to "git push" again:

Switch to branch "foo" and update it:

$ git checkout foo
$ git pull

You can see the branches you've got by issuing command:

$ git branch
Zds
  • 4,311
  • 2
  • 24
  • 28
David Calhoun
  • 8,315
  • 4
  • 30
  • 23
  • Can you explain why this works and is necessary? (It did solve my problem.) It just seems counter-intuitive to me. I don't understand why git would need another branch to be up to date as well for me to push on the master branch. – Quinxy von Besiex Feb 18 '15 at 04:09
  • @QuinxyvonBesiex I'm not sure I myself understand. It may have something to do with the underlying structure of Git itself, and how it organizes branches (which are basically the same as tags as far as I understand it). – David Calhoun Feb 18 '15 at 23:53
7

You can force it to push, but please do this ONLY when you're quite sure what you are doing.

The command is:

git push -f 
George Netu
  • 2,758
  • 4
  • 28
  • 49
JuLy
  • 483
  • 2
  • 5
  • 12
3

This problem can also occur when you have conflicting tags. If your local version and remote version use same tag name for different commits, you can end up here.

You can solve it my deleting the local tag:

$ git tag --delete foo_tag
Zds
  • 4,311
  • 2
  • 24
  • 28
2

When I got this error, I backed up my entire project folder. Then I did something like

$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master

...depending on your branch name (if it's not master).

Then I did git pull --rebase. After that, I replaced the pulled files with my backed-up project's files. Now I am ready to commit my changes again and push.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147