rejected master -> master (non-fast-forward)
I'm trying to push my project (all files in a new repository). I follow the steps but when I push with git push -u origin master
I get this error:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:asantoya/projectnewbies.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
I got this error many times and can't figure out what to do.
Solution 1:
NOTICE: This is never a recommended use of git. This will overwrite changes on the remote. Only do this if you know 100% that your local changes should be pushed to the remote master.
⚠️ Try this: git push -f origin master
Solution 2:
As the error message says: git pull
before you try to git push
. Apparently your local branch is out of sync with your tracking branch.
Depending on project rules and your workflow you might also want to use git pull --rebase
.
Solution 3:
I've just received this error.
I created a github repository after creating my local git repository so I needed to accept the changes into local before pushing to github. In this case the only change was the readme file created as optional step when creating github repository.
git pull https://github.com/*username*/*repository*.git master
repository URL is got from here on project github page :
I then re-initialised (this may not be needed)
git init
git add .
git commit -m "update"
Then push :
git push
Solution 4:
use this command:
git pull --allow-unrelated-histories <nick name of repository> <branch name>
like:
git pull --allow-unrelated-histories origin master
this error occurs when projects don't have any common ancestor.
Solution 5:
If git pull
does not help, then probably you have pushed your changes (A) and after that had used git commit --amend
to add some more changes (B). Therefore, git thinks that you can lose the history - it interprets B as a different commit despite it contains all changes from A.
B
/
---X---A
If nobody changes the repo after A
, then you can do git push --force
.
However, if there are changes after A
from other person:
B
/
---X---A---C
then you must rebase that persons changes from A
to B
(C
->D
).
B---D
/
---X---A---C
or fix the problem manually. I didn't think how to do that yet.