Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot push to git repository

Tags:

git

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?

like image 655
Serhat Ozgel Avatar asked Dec 28 '10 21:12

Serhat Ozgel


People also ask

Why is push not working in git?

If git push origin master not working , all you need to do is edit that file with your favourite editor and change the URL = setting to your new location. Assuming the new repository is correctly set up and you have your URL right, you'll easily be able to push and pull to and from your new remote location.

Why can't I push code to GitHub?

To push a branch on remote, your branch needs to have the latest changes present in remote repository. If you get the failed to push error, first do git pull the branch to get the latest commits and then push it.

Why push command is not working?

The git push command gets rejected because of following reasons: It may be possible that push permission is not provided to the user pushing the code changes. I suggest checking the user permissions.


2 Answers

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

like image 122
Michal Čihař Avatar answered Oct 06 '22 11:10

Michal Čihař


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.

like image 38
Kaikass Avatar answered Oct 06 '22 10:10

Kaikass