Serhat Ozgel
Serhat Ozgel

Reputation: 23766

Cannot push to git repository

I keep getting this error message from git while pushing, even I am trying it after pulling over and over again:

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[repo url]'
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.

Here is what the branch history looks like:

A---B---C
  |   |
  D   E-F

A initial commit (me)
B some commit (me)
C master
D some commit (other dev)
E remotes/origin/master - Merge branch 'master' of [repo url]
F Local uncommitted changes, not checked into an index

From here, when I pull, nothing comes. When I push, I get the error. How can I successfully push again?

Upvotes: 16

Views: 16355

Answers (3)

Kaikass
Kaikass

Reputation: 91

I had this exact problem, it was due to the following:

A local branch in my machine (with different origin) that was not updated (and had been modified by another user). You must move to the branch that has not been updated (git checkout [branch in question]) and do a git pull.

That solved my problem. In this particular case, due to this message

"! [rejected] master -> master (non-fast-forward)!"

You must move to the master branch (git checkout master) do a git pull, then move back to whatever branch you are working on (git checkout whateverBranch) and now you should be able to do a push without problems.

I'm not positive on why this happens; it may be that the nature of git push is to check all local branches that are not properly updated when doing a push.

Upvotes: 5

Steve Johnson
Steve Johnson

Reputation: 3114

The solution to your problem is:

  1. "Commit" the changes so that these are committed to your local repository.
  2. Then "Pull" the code from github/git server
  3. Then "Push" the code to github/git server and you will not face any issue.

Read more under heading "Pushing a Branch" in the following url:

Dealing with "non-fast forward rejects"

Hope it helps.

Thanks

Upvotes: 4

Michal Čihař
Michal Čihař

Reputation: 10091

You need to have common ancestor before pushing, the easiest way is to do git pull (or git pull --rebase if you want to avoid merge commit and rebase is not an issue for you).

Upvotes: 6

Related Questions