I'm using Git to manage my two computers and my development. I'm trying to commit changes to GitHub, and I got this error:
Failed to push some refs to
<repo>
. To prevent you from losing history, non-fast-forward updates were rejected. Merge remote changes before pushing again.
What could be causing this, and how can I fix this?
EDIT:
Pulling the repo returns the following:
*branch master->master (non-fast-forward) Already-up-to-date
Pushing still gives me the aforementioned error.
This question is related to
git
version-control
A fast-forward update is where the only changes one one side are after the most recent commit on the other side, so there doesn't need to be any merging. This is saying that you need to merge your changes before you can push.
you might want to use force with push operation in this case
git push origin master --force
Never do a git -f
to do push
as it can result in later disastrous consequences.
You just need to do a git pull
of your local branch.
Ex:
git pull origin 'your_local_branch'
and then do a git push
It means that there have been other commits pushed to the remote repository that differ from your commits. You can usually solve this with a
git pull
before you push
Ultimately, "fast-forward" means that the commits can be applied directly on top of the working tree without requiring a merge.
You need to merge and resolve the conflicts locally
before you push your changes to remote repo/fork.
1) pull (fetch and merge)
$ git pull remote branch
2) Push the changes
$ git push remote branch
Still you have a quick choice to push
forcibly by using --force
option but should be avoided as it may result in changes loss or affect badly on other contributors.
In my case for exact same error, I was also not the only developer.
So I went to commit & push my changes at same time, seen at bottom of the Commit
dialog popup:
...but I made the huge mistake of forgetting to hit the Fetch
button to see if I have latest, which I did not.
The commit successfully executed, however not the push, but instead gives the same mentioned error; ...even though other developers didn't alter same files as me, I cannot pull latest as same error is presented.
Most of the time I prefer sticking with Sourcetree's GUI (Graphical User Interface). This solution might not be ideal, however this is what got things going again for me without worrying that I may lose my changes or compromise more recent updates from other developers.
Right-click on the commit right before yours to undo your locally committed changes and select Reset current branch to this commit
like so:
Once all the loading spinners disappear and Sourcetree is done loading the previous commit, at the top-left of window, click on Pull
button...
...then a dialog popup will appear, and click the OK
button at bottom-right:
After pulling latest, if you do not get any errors, skip to STEP 4 (next step below). Otherwise if you discover any merge conflicts at this point, like I did with my Web.config
file:
...then click on the Stash
button at the top, a dialog popup will appear and you will need to write a Descriptive-name-of-your-changes, then click the OK
button:
...once Sourcetree is done stashing your altered file(s), repeat actions in STEP 2 (previous step above), and then your local files will have latest changes. Now your changes can be reapplied by opening your STASHES
seen at bottom of Sourcetree left column, use the arrow to expand your stashes, then right-click to choose Apply Stash 'Descriptive-name-of-your-changes'
, and after select OK
button in dialog popup that appears:
IF you have any Merge Conflict(s) right now, go to your preferred text-editor, like Visual Studio Code, and in the affected files select the Accept Incoming Change
link, then save:
Then back to Sourcetree, click on the Commit
button at top:
then right-click on the conflicted file(s), and under Resolve Conflicts
select the Mark Resolved
option:
Finally!!! We are now able to commit our file(s), also checkmark the Push changes immediately to origin
option before clicking the Commit
button:
P.S. while writing this, a commit was submitted by another developer right before I got to commit, so had to pretty much repeat steps.
Source: Stackoverflow.com