Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, November 30, 2021

[Fixed] Git error: failed to push some refs to in 5 ways

1. Overview

In this tutorial, We'll learn how to fix the git error failed to push some refs to in 5 ways.

Most of the time you get this error while pushing your change git remote repository.

This might be working a few days back but suddenly started showing a weird error message "failed to push some refs to".

You are stuck at this point and do not what to do now. And you might be thinking because of my new changes?


For this error, there are mainly two reasons.

a) Your branch is set to main and not master
b) Remote repo has new changes and you do not have those changes on your laptop or local branch

Now, you knew about the actual reasons to get this common error.

Let us jump into the fixes.

Git error: failed to push some refs to




2. Fix 1 Heroku - Git error: failed to push some refs to


To solve this error, you need to check what is the default branch on your github.com.

Once you find the right branch then you need to switch to the correct branch using git push command.

If the default branch is main on remote but it is master on your branch then please run below git push command.

git push heroku main 

Otherwise, it could be master as below.
git push heroku master 


3. Fix 2 - Git error: failed to push some refs to


If some else have pushed the new changes before you push to the remote repo. In this case, you do not have the recent commits on your laptop local repo.


So, you need to first pull the changes from that branch and then push your new commits.

To fix this error, follow the below steps.

git pull origin <your-branch>
git push origin <your-branch>

It is strongly recommended to pull always changes first before pushing your commits to remote origin.



4. Fix 3 - Git error: failed to push some refs to


Sometimes, fix 2 did not work if you have more commits that produce the conflicts. In this case, the normal git pull command won't work properly.

The solution to this problem is git-rebase.

Git rebase is a process of moving and arranging the sequence of comments into the new base commit.

Follow the below commands. Here the branch can be modified based on your branch.

Branch name can be master or main or another branch name.

git pull -rebase origin <your-branch-name>
git push origin <your-branch-name>



5. Fix 4 - Git error: failed to push some refs to


You can try force push also. But not sure this will work always. If there are no conflicts, this will work.

git push -f origin master


6. Fix 5 - Git error: failed to push some refs to


the last solution is below. That deregister the current local repo and re-register with the remote.

rm -rf .git

git init

git remote add origin https://github.com/JavaProgramTo/Kotlin-Demo.git

git remote -v (to see the remote origin)

git add -A(add all files)

git commit -m 'Added my project'

git pull --rebase origin master

git push  origin master


7. Conclusion


In this article, We've seen how to fix the git error - failed to push some refs to.


No comments:

Post a Comment

Please do not add any spam links in the comments section.