I started to get \'failed to push some refs\' error when I changed some files and tried to do push. Most instructions tell to do git pull first. I have done it and git says ever
try this command git push -f origin master
Be sure to verify that the pull actually worked. It is possible the merge (which is part of a pull) failed.
When I had this error message I failed to notice the pull failed. The pull failed due to some uncommitted file changes (unrelated to the files I was trying to push) which caused the merge to fail. I reverted those file changes (because they were not important) and then did a pull again. After the successful pull, the push worked.
Be sure your branch is unprotected, or developer can push is checked.
The best possible answer is on Git help here
Dealing with non-fast-forward errors
From time to time, you may encounter this error while pushing commits (git push origin master
) to GitHub:
To https://github.com/user/repo.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/user/repo.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.
This error can be a bit overwhelming at first; do not fear!
Simply put, Git can't make the change on the remote without losing commits, so it refuses the push. Usually, this is caused by another user pushing to the same branch.
You can remedy this by fetching and merging the remote branch:
git fetch origin
git merge origin master
Or, you can simply use git pull to perform both commands at once:
git pull origin master
In some cases, this error is a result of destructive changes made locally by using commands like git commit --amend
or git rebase
. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don't force-push.
Also, the error happens when you put the wrong branch name into your push command.
For example, say you're on a branch called iss8
, but you tell git to push to iss3
(like I just did), you'll get the same error.
$ git push origin iss3
error: src refspec iss3 does not match any.
error: failed to push some refs to 'git@myhost.com:myuser/myproject.git'
Hmm, check status:
$ git status
On branch iss8
nothing to commit, working directory clean
Oh, look, I'm on a different branch. Thanks git for not letting me do something really dumb.
$ git push origin iss8
Counting objects: 15, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 2.62 KiB | 0 bytes/s, done.
Total 9 (delta 3), reused 0 (delta 0)
To git@myhost.com:myuser/myproject.git
* [new branch] iss8 -> iss8
1) git stash
2) git pull
3) git push
My Original Error:
After "git stash"