git push rejected: error: failed to push some refs
GitGit PushGit PullGit Problem Overview
I know people have asked similar questions, but I believe the causes of their problems to be different. I did a hard reset because I had messed up my code pretty bad
git reset --hard 41651df8fc9
I've made quite some changes, I've made some commits and now that I'm trying to push all these commits into the server I get the following error:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]'
Git suggests to do a git pull and that's what other people have suggested to other users. However, I believe that a git pull will merge my current code with the code that I don't want anymore (head revision). How can I do a push and forget about the version/revisions ahead of me?
Git Solutions
Solution 1 - Git
git push -f
if you have permission, but that will screw up anyone else who pulls from that repo, so be careful.
If that is denied, and you have access to the server, as canzar says below, you can allow this on the server with
git config receive.denyNonFastForwards false
Solution 2 - Git
If you are the only the person working on the project, what you can do is:
git checkout master
git push origin +HEAD
This will set the tip of origin/master to the same commit as master (and so delete the commits between 41651df and origin/master)
Solution 3 - Git
Just do
git pull origin [branch]
and then you should be able to push.
If you have commits on your own and didn't push it the branch yet, try
git pull --rebase origin [branch]
and then you should be able to push.
Read more about handling branches with Git.
Solution 4 - Git
> 'remote: error: denying non-fast-forward refs/heads/master (you should > pull first)'
That message suggests that there is a hook on the server that is rejecting fast forward pushes. Yes, it is usually not recommended and is a good guard, but since you are the only person using it and you want to do the force push, contact the administrator of the repo to allow to do the non-fastforward push by temporarily removing the hook or giving you the permission in the hook to do so.
Solution 5 - Git
What I did to solve the problem was:
git pull origin [branch]
git push origin [branch]
Also make sure that you are pointing to the right branch by running:
git remote set-url origin [url]
Solution 6 - Git
for me following worked, just ran these command one by one
> git pull -r origin master > > git push -f origin your_branch
Solution 7 - Git
I did the following steps to resolve the issue. On the branch which was giving me the error:
git pull origin [branch-name]<current branch>
- After pulling, got some merge issues, solved them, pushed the changes to the same branch.
- Created the Pull request with the pushed branch... tada, My changes were reflecting, all of them.