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?
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?
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.
Using the --rebase option worked for me.
git pull <remote> <branch> --rebaseThen push to the repo.
git push <remote> <branch>E.g.
git pull origin master --rebase
git push origin master
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