Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
3 views
in DevOps and Agile by (27.5k points)
edited by
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 <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.

1 Answer

0 votes
by (19.4k points)

Alright let us break this overwhelming issue down. 

Since, git can't make the change on the remote without losing commits, so it refuses the push.

In general, 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 destructive changes made locally by using commands like git commit --amend or git rebase.

Yes, you can override 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. 

The following message occurs due to the local branches which don't track their remote counter-part 

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

To solve this what you can do is:

Before performing push, perform pull using the following command: 

$ git pull

These git commands are frequently used while working with Git.

Browse Categories

...