! [rejected] master -> master (non-fast-forward) - What does “Git push non-fast-forward updates were rejected” mean?

859    Asked by AndreaBailey in Devops , Asked on Jun 1, 2021

I'm using Git to manage my two computers and my development. I'm trying to commit changes to GitHub and I'm getting the error.


Failed to push some refs to . 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.

Answered by Andrea Bailey

To solve the rejected master master non fast forward error let’s break this overwhelming issue down.

Since, git can't 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. In that case, fetching and merging the remote branch, or using pull to perform both at once would make things right.

Whereas, in other cases this error is a result of devastative changes made locally by using various commands like git commit --amend or git rebase.

Yes, you can disallow the remote by adding --force to the push command, but you should only do so if you are absolutely certain this is what you want to do, because force pushes can cause issues for other users that have fetched the remote branch, and it is considered as a bad practice.

However, the following message occurs due to the local branches which don't track their remote counterpart.

    "branch master->master (non-fast-forward) Already-up-to-date".

Moreover, to solve this what you can do is:

Before performing push, perform pull using the following command:

    $ git pull


Your Answer

Interviews

Parent Categories