105

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?

Sarath
  • 9,030
  • 11
  • 51
  • 84
  • Duplicate of [What's a "fast-forward" in Git?](http://stackoverflow.com/questions/4684352/whats-a-fast-forward-in-git) –  May 18 '14 at 17:02

12 Answers12

139

Pull changes first:

git pull origin branch_name
trejder
  • 17,148
  • 27
  • 124
  • 216
iafonov
  • 5,152
  • 1
  • 25
  • 21
  • 1
    If that doesn't solve your problem, make sure that you are pushing to the same branch as the one on which you are currently working on. Check on which branch you are on with "git status". – afilina Dec 15 '13 at 23:53
  • 1
    This command worked me, however I'd like to know, why this doesn't: `git pull`? The remote is equal to `origin`, so it actually runs: `git pull origin`. Shouldn't it update all the branches? – Karlen Kishmiryan Apr 15 '15 at 13:35
85

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/

Matt
  • 983
  • 7
  • 2
  • 10
    --force will solve your problems but potentially harm others. It should be used only with much care (and knowledge) – schoetbi Sep 03 '13 at 04:38
  • 8
    -1 because forcing pushes *generally speaking* is a terrible idea. – joshin4colours Oct 24 '13 at 18:39
  • 6
    +1 because 5 people agreed with @joshin4colours to give -1. But while force pushing is not always the best idea (which git makes quite clear by denying your push), if it was a bad idea 100% of the time, the option would not exist. Matt's suggestion here can certainly be useful to others. – Nike Apr 27 '17 at 01:51
  • 3
    Feel free to use the `--force` if you are the only one using that branch. It does cause issues when sharing a branch with other developers though. – Robert Brisita Aug 22 '18 at 09:04
  • `--force` **is destructive** because it unconditionally overwrites the remote repository with whatever you have locally. But `--force-with-lease` ensure you don't overwrite other's work. See more info here https://stackoverflow.com/questions/6897600/git-push-failed-non-fast-forward-updates-were-rejected/51447578#51447578 – igor.js May 18 '22 at 02:04
27

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.

trejder
  • 17,148
  • 27
  • 124
  • 216
satishgoda
  • 398
  • 1
  • 4
  • 8
  • 2
    In my case `git pull --rebase` ends up with `There is no tracking information for the current branch. Please specify which branch you want to rebase against.` – trejder Jul 02 '13 at 07:56
16

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

git push origin master --force
dwo
  • 3,576
  • 2
  • 22
  • 39
Wen Qi
  • 655
  • 7
  • 9
  • 7
    are there consequences to this ? – jayunit100 May 25 '12 at 03:40
  • 9
    losing commits of others. – Giovanni Toraldo Aug 29 '13 at 15:16
  • 1
    I had an oddball situation where this is **exactly** what I wanted to do... blow away the contents of a just-created remote master branch with something new. This solved my problem. While it isn't the solution for everyone, `--force` can be helpful. – Brad Jan 27 '15 at 02:42
  • 1
    I don't think this answer deserves to be down voted 6 times. This is a valid solution to the provided problem, however, the author could have been a bit more descriptive regarding the circumstances around which this command would be useful. If it worth mentioning since it was worth writing (the functionality for --force) – Aiden Strydom Mar 05 '15 at 22:03
  • `--force` **is destructive** because it unconditionally overwrites the remote repository with whatever you have locally. But `--force-with-lease` ensure you don't overwrite other's work. See more info here https://stackoverflow.com/questions/6897600/git-push-failed-non-fast-forward-updates-were-rejected/51447578#51447578 – igor.js May 18 '22 at 02:06
13

The safest way to solve this is using --rebase

E.g.

git pull <remote> <branch> --rebase

This may cause conflicts in your local branch, and you will need to fix them manually.

Once you resolve all the conflicts, you can push your change 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 while you were rebasing, the push is rejected, and you will be forced to rebase your branch again.

It is annoying if you are working on a large project with hundreds of commits every hour, 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 on this post in Atlassian's developer blog.

igor.js
  • 145
  • 1
  • 7
5

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.

hardmooth
  • 1,202
  • 2
  • 19
  • 36
2

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

CyberDemic
  • 180
  • 2
  • 6
1

(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.

Sliq
  • 15,937
  • 27
  • 110
  • 143
0

I've hade the same problem. I resolved with

git checkout <name branch>
git pull origin <name branch>
git push origin <name branch>
Andrea Perdicchia
  • 2,786
  • 1
  • 20
  • 19
  • 3
    OP says about pushing local code changes. `checkout` will overwrite these changes or at least won't include them in push. – trejder Jul 02 '13 at 07:50
0

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
CodeChops
  • 1,980
  • 1
  • 20
  • 27
0

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.

0

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
Abhinav
  • 34
  • 2
  • 8