开发者

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

开发者 https://www.devze.com 2023-02-27 08:45 出处:网络
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

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)开发者_Go百科:

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.


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


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号