When working in collabration with other developers using Git we might sometimes come accross this “error : failed to push some refs to [remote repo]” error.
In this article, we will see what causes this git error and check different method to resolve this “git failed to push refs” error.
What causes the “failed to push some refs to” error?
Before going to the solution to fix this error, let understand why this error occurs in the first place. The error message “failed to push some refs” usually appears when we attempt to push our local changes to the remote repository (repo) while the local repo has not yet been updated with the changes that were made in the remote repository by other developers.
This error basically tells us to first update our local repo with the remote repository changes before pushing our own changes.
The other reasons that might cause this error are:
- You are trying to push to a remote repo which is now being deleted.
- You have not added the git remote origin to your local repo.
So now let’s discuss three possible ways to resolve this error below.
git pull to fix “error: failed to push some refs to” in Git
git pull command usually fix this error easily in most cases in Git. Using
git pull we request to fetch all the new changes that were made in the remote repository to our local repo.
git pull origian [branch name]
For example, we want to push our local change to the remote
master branch, so before doing that, we will make a pull request to fetch all the new changes from the remote master branch.
git pull origin master git push origin master
git push origin master command pushes the latest changes from the updated local repo to the remote repo.
If you are working on different branch then you have to use that branch name instead of the
2. Using git pull –rebase to fix “failed to push some refs to” error in Git
git pull --rebase is use when the local branch commit is behind the remote branch. The command is the combination of
git fetch and
git rebase command.
In simple words,
git pull --rebase will fetch all the lastest changes from the remote branch and apply it to the local changes using rebase instead of merge. It helps to keep the Git history linear without merge conflicts.
So we can use the
git pull --rebase command to fix the “failed to push some refs to” error like this.
git pull --rebase origin [barnch name] git push origin [branch name]
git push command pushes the new changes to the remote repo.
Onces the task succeds then you will get “Successfully rebased and updated refs/heads/[branch name]” in your terminal.
3. Remove and add remote origin
Now, if the above two solution do not work for you then try to remove and add the remote orgin again to your local repo.
To remove remote from your local repo, use this following command:
git remote remove origin
Next, to add it again use this command:
git remote add origin yourRemoteUrl
And then finally use the
git push command to push the latest changes to remote repo.
git push origin [branch name]
Here, we have learned what is the cause the “Error:failed to push some refs to” error in Git.
The simple fixed to this error is by fetching the changes from the remote repo first using
git pull origin [branch name] and then pushing the local changes to remote using
git push origin [branch name] command.