15

git push origin master shows an error

failed to push some refs to 'git@github.com:xyz/abc.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.

What is this? How to recover this?

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
user12345
  • 2,400
  • 8
  • 33
  • 40
  • 2
    Did you "see the 'Note about fast-forwards' section of 'git push --help' for details"? It's a bit too lengthy to reproduce here, but it describes the exact problem, along with ascii-art diagrams – Michael Mrozek Aug 30 '10 at 06:37

6 Answers6

30

See the "pushing a branch" section from GitHub help page:

Dealing with “non-fast-forward” errors

From time to time you may encounter this error while pushing:

$ git push origin master
To ../remote/
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '../remote/'
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.

This error can be a bit overwhelming at first, do not fear.
Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch.
You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.

In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase.
While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.


Mode details on the "Note about fast-forwards" of git push, as mentioned by Michael Mrozek in the comments.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
7

In addition to VonC's answer,

In case you, intend to override the remote changes with your local,

  $git push --force

will do.

Intrications
  • 16,782
  • 9
  • 50
  • 50
Joe Lewis
  • 1,970
  • 2
  • 18
  • 20
  • As VonC's answer says "While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do." – Intrications Feb 08 '13 at 11:37
3

Quite important: this can also occur if name of the branch you are on (and want to merge) and the branch upstream are not the same. Then work flow could look like this:

git pull --rebase             # to retrieve upstream changes, replay yours on top
git push --dry-run --verbose origin HEAD:master   # always good idea: dry-runs..
git push origin HEAD:master   # actually push current branch head (non-master) to upstream master
eMPee584
  • 1,945
  • 20
  • 20
2

First Pull then push

git pull origin any_branch_name

Arjun
  • 815
  • 1
  • 8
  • 18
2

If you are getting this error and your .git/config file is just fine then go through these steps (this is a common problem with a submodule in git repos - again do this from the submodule dir)

  1. git checkout master
  2. git pull
  3. (merge happens in background)
  4. git push
David Yaw
  • 27,383
  • 4
  • 60
  • 93
oldDevGuy
  • 41
  • 2
  • i'm not too sure what you are trying to say here? please explain more clearly. – Steve P Nov 09 '12 at 14:41
  • He is trying to see if you are try to push from a branch and getting reject message , follow the steps and all will be well... This worked for me like a champ... Thanks jclaan... yes it did the merge in the background – z atef Oct 02 '15 at 15:29
0

I had this too and couldn't find anywhere on StackOverflow with the following useful nugget clearly stated: Git won't let you push to a different branch if your working branch has diverged from it. There's an easy fix - just switch to the branch you want to push to and merge your working branch into it. So instead of trying this:

git push origin master   <========== On "mybranch"

Do this:

git checkout master      <========== Switch to the branch you want to push to
git pull origin master   <========== Get latest from remote repository
git pull origin mybranch <========== Merge in changes from "mybranch"
======== Resolve any issues ========
git push origin master   <========== Push the merged changes
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208