Git push failed, "Non-fast forward updates were rejected"

GitGithubPush

Git Problem Overview


I've edited my GIT repositories via Git Online. After I tried to push my local code changes, I got an error:

Git push failed, To prevent from losing history, non-fast forward updates were rejected.

How can I fix this?

Git Solutions


Solution 1 - Git

Pull changes first:

git pull origin branch_name

Solution 2 - Git

Add --force to your command line if you are sure you want to push. E.g. use git push origin --force (I recommend the command line as you will find much more support from other users with the command line. Also this may not be possible with SmartGit.) See this site for more information: http://help.github.com/remotes/

Solution 3 - Git

Before pushing, do a git pull with rebase option. This will get the changes that you made online (in your origin) and apply them locally, then add your local changes on top of it.

git pull --rebase

Now, you can push to remote

git push 

For more information take a look at Git rebase explained and Chapter 3.6 Git Branching - Rebasing.

Solution 4 - Git

I encountered the same error, just add "--force" to the command, it works

git push origin master --force

Solution 5 - Git

The safest way to solve this is using --rebase

E.g.

git pull <remote> <branch> --rebase

This probably will cause conflicts on your local branch and you will need to fix them manually.

Once you resolve all the conflicts you can push your changed with --force-with-lease

E.g.

git push <remote> <branch> --force-with-lease

Using this flag, git checks if the remote version of the branch is the same as the one you rebase, i.e. if someone pushed a new commit when you rebased, the push is rejected and you're forced to rebase your branch again.

It is a bit annoying if you are working on a large project with hundreds of commits every minute but it is still the best way to solve this problem.

AVOID USING --force, unless you know exactly what you are doing.

Using --force is destructive because it unconditionally overwrites the remote repository with whatever you have locally.

But with --force-with-lease ensure you don't overwrite other's work.

See more info here.

Solution 6 - Git

I've had the same problem.
The reason was, that my local branch had somehow lost the tracking to the remote counterpart.

After

git branch branch_name --set-upstream-to=origin/branch_name
git pull

and resolving the merging conflicts, I was able to push.

Solution 7 - Git

(One) Solution for Netbeans 7.1: Try a pull. This will probably also fail. Now have a look into the logs (they are usually shown now in the IDE). There's one/more line saying:

"Pull failed due to this file:"

Search that file, delete it (make a backup before). Usually it's a .gitignore file, so you will not delete code. Redo the push. Everything should work fine now.

Solution 8 - Git

Using the --rebase option worked for me.

  • git pull <remote> <branch> --rebase

Then push to the repo.

  • git push <remote> <branch>

E.g.

git pull origin master --rebase

git push origin master

Solution 9 - Git

I've hade the same problem. I resolved with

git checkout <name branch>
git pull origin <name branch>
git push origin <name branch>

Solution 10 - Git

This is what worked for me. It can be found in git documentation here

If you are on your desired branch you can do this:

git fetch origin
# Fetches updates made to an online repository
git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work

Solution 11 - Git

Encountered the same problem, to solve it, run the following git commands.

  • git pull {url} --rebase
  • git push --set-upstream {url} master

You must have created the repository on github first.

Solution 12 - Git

Sometimes, while taking a pull from your git, the HEAD gets detached. You can check this by entering the command:

git branch 

>>* (HEAD detached from 8790704)

>> master

>> develop

It's better to move to your branch and take a fresh pull from your respective branch.

git checkout develop

git pull origin develop

git push origin develop

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSarathView Question on Stackoverflow
Solution 1 - GitiafonovView Answer on Stackoverflow
Solution 2 - GitMattView Answer on Stackoverflow
Solution 3 - GitsatishgodaView Answer on Stackoverflow
Solution 4 - GitWen QiView Answer on Stackoverflow
Solution 5 - Gitigor.jsView Answer on Stackoverflow
Solution 6 - GithardmoothView Answer on Stackoverflow
Solution 7 - GitSliqView Answer on Stackoverflow
Solution 8 - GitCyberDemicView Answer on Stackoverflow
Solution 9 - GitAndrea PerdicchiaView Answer on Stackoverflow
Solution 10 - GitCodeChopsView Answer on Stackoverflow
Solution 11 - GitblackFoxCoderView Answer on Stackoverflow
Solution 12 - GitAbhinavView Answer on Stackoverflow