Tag Archives: non-fast-forward

Conflict resolution after git error non fast forward

When the code is to be pushed to git, a prompt appears.

error:failed to push some refs to …

Dealing with “non-fast-forward” errors
From time to time you may encounter this error while pushing:

$gitpushoriginmaster

To../remote/

![rejected]master->master(non-fastforward)

error:failedtopushsomerefsto’../remote/’

To prevent you from losing history, non-fast-forward updates were rejected

Merge the remote changes before pushing again. See the ‘non-fast forward’

section of ‘git push –help’ for details.

This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.

In other cases this error is a result of destructive changes made locally by using commands like git commit –amend or git rebase. While you can override the remote by adding –force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.

The reason for the problem (Non-fast-forward) is that the git repository already has some of the code in it, so it doesn’t allow you to overwrite your code directly. So you have 2 options.

1, force-push, which replaces the git repository with your local code using a strong override

git push -f

2, fetch the git stuff to your local repository and then merge it and then push it

$ git fetch

$ git merge

These two commands are equivalent to

$git pull

But then you have the following problem.

The [branch “master”] that appears above is required to be explicit (.git/config) as follows

[branch “master”]

remote = origin

 

merge = refs/heads/master

This tells git 2 things:

 

1, when you are on master branch, the default remote is origin.

2, when you use git pull on master branch without specifying remote and branch, then git will use the default remote (that is, origin) to merge all changes on master branch

 

If you don’t want to or don’t know how to edit the config file, you can type the following command line on bush.

$gitconfigbranch.master.remoteorigin

$gitconfigbranch.master.mergerefs/heads/master

After that, git pull again. Finally git push your code.

it works now~