Git push complaining about non-fast-forward, even though remote has been pulled

夙愿已清 提交于 2019-12-21 05:19:14

问题


I'm trying to push my changes to a repo on my NAS. It's failing in a way I don't understand.

The documentation states that by default push works only with fast-forward updates. Fair enough. So I do a git pull (my remote is called rubix):

D:\RoboCup\Dev\TinMan>git pull rubix master
From ssh://rubix/volume1/git/TinMan
 * branch            master     -> FETCH_HEAD
Already up-to-date.

All looks well. Let's try pushing...

D:\RoboCup\Dev\TinMan>git push rubix master
To ssh://dnoakes@rubix/volume1/git/TinMan
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://dnoakes@rubix/volume1/git/TinMan'
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.

I've read through the documentation on git push but at this point I can't understand why I'm seeing this problem.

Here is some other contextual info:

D:\RoboCup\Dev\TinMan>git --version
git version 1.7.0.2.msysgit.0

D:\RoboCup\Dev\TinMan>git branch
* (no branch)
  master

That last line looks suspect. How can I not be on any branch? Note too that I have some untracked files and modifieds (unstaged) changes too.

Any help would be greatly appreciated. Thanks.


回答1:


Here's one way to get synced up.

First, Commit any changes, then use git log to note any commits you wanted to push.

Next reset your master to match the remote like this:

git checkout master
git reset --hard remotes/rubix/master 

Finally, cherry pick the commits you wanted to keep

Example:

git cherry-pick 111aaa111
git cherry-pick 123abc123

Now pushing should work.

git push rubix master



回答2:


I'm not sure how you ended up on no branch. You may be in the middle of a rebase. You may have checked out a commit directly. In any event, this is likely the cause of your pushing woes. Check git reflog to see if it shows any obvious cause and git log to see where you are. Look at the changed files with git diff to decide if you want to keep them (and use git add . && git stash if you do). Then, once you are sure you don't need any changes or any of your current history, git checkout master to get back.




回答3:


If it still not work, you may probably need to configure your git to accept alias branch name

git config --global push.default upstream

By default, if your remote branch name is origin/master, git will only accept your push if your local tracking branch name is master. If you create a local tracking branch as master_xxx, git will not accept your push unless you modify your push.default configuration to upstream.




回答4:


I had this same problem and came across this question, so I thought I would post the solution to my problem.

NOTE: I am using git on Windows.

I had accidentally checked out my local branch with different casing: "Develop" instead of my usual "develop." Nothing I did would correct the issue (even after checking out the branch with the correct casing) until I followed these steps:

  1. Checkout out the branch with the correct casing "develop"
  2. Make any change, add/commit
  3. Now you can push without being rejected


来源:https://stackoverflow.com/questions/5723714/git-push-complaining-about-non-fast-forward-even-though-remote-has-been-pulled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!