简体   繁体   中英

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?

你需要在推送之前拥有共同的祖先,最简单的方法是执行git pull (或git pull --rebase如果你想避免合并提交,并且rebase对你来说不是问题)。

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM