(Note: starting Oct. 2020, any new repository is created with the default branch main, not master. And you can rename existing repository default branch from master to main.
The rest of this 2014 answer has been updated to use «main«)
(The following assumes github.com itself is not down, as eri0o points out in the comments: see www.githubstatus.com to be sure)
If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using:
git pull --rebase
git push
The full syntax is:
git pull --rebase origin main
git push origin main
With Git 2.6+ (Sept. 2015), after having done (once)
git config --global pull.rebase true
git config --global rebase.autoStash true
A simple git pull would be enough.
(Note: with Git 2.27 Q2 2020, a merge.autostash is also available for your regular pull, without rebase)
That way, you would replay (the --rebase part) your local commits on top of the newly updated origin/main (or origin/yourBranch: git pull origin yourBranch).
See a more complete example in the chapter 6 Pull with rebase of the Git Pocket Book.
I would recommend a:
# add and commit first
#
git push -u origin main
# Or git 2.37 Q2 2022+
git config --global push.autoSetupRemote true
git push
That would establish a tracking relationship between your local main branch and its upstream branch.
After that, any future push for that branch can be done with a simple:
git push
Again, with Git 2.37+ and its global option push.autoSetupRemote, a simple git push even for the first one would do the same (I.e: establishing a tracking relationship between your local main branch and its upstream branch origin/main).
See «Why do I need to explicitly push a new branch?».
Since the OP already reset and redone its commit on top of origin/main:
git reset --mixed origin/main
git add .
git commit -m "This is a new commit for what I originally planned to be amended"
git push origin main
There is no need to pull --rebase.
Note: git reset --mixed origin/main can also be written git reset origin/main, since the --mixed option is the default one when using git reset.

When collaborating with other developers using Git, you might encounter the error: failed to push some refs to [remote repo] error.
This error mainly occurs when you attempt to push your local changes to GitHub while the local repository (repo) has not yet been updated with any changes made in the remote repo.
So Git is trying to tell you to update the local repo with the current changes in the remote before pushing your own changes. This is necessary so that you don’t override the changes made by others.
We’ll be discussing two possible ways of fixing this error in the sections that follow.
We can fix the error: failed to push some refs to [remote repo] error in Git using the git pull origin [branch] or git pull --rebase origin [branch] commands. In most cases, the latter fixes the error.
Let’s go over how you can use the commands above.
How to Fix error: failed to push some refs to Error in Git Using git pull
To send a pull request means to «fetch» new changes made to the remote repo and merge them with the local repo.
Once the merging is done, you can then push your own code changes to GitHub.
In our case, we’re trying to get rid of the error: failed to push some refs to [remote repo] error by sending a pull request.
Here’s how you can do that:
git pull origin main
If you’re working with a different branch, then you’d have to replace main in the example above with the name of your branch.
Just keep in mind that there are chances of failure when using this command to sync your remote and local repos to get rid of the error. If the request succeeds, then go on and run the command below to push your own changes:
git push -u origin main
If the error persists, you’ll get an error that says: fatal: refusing to merge unrelated histories. In that case, use the solution in the next section.
How to Fix error: failed to push some refs to Error in Git Using git pull --rebase
The git pull --rebase command is helpful in situations where your local branch is a commit behind the remote branch.
To fix the error, go on and run following commands:
git pull --rebase origin main
git push -u origin main
If the first command above runs successfully, you should get a response that says: Successfully rebased and updated refs/heads/main.
The second command pushes your local repo’s current state to the remote branch.
Summary
In this article, we talked about the error: failed to push some refs to [remote repo] error.
This error occurs when you attempt to push your local changes to the remote repo without updating your local repo with new changes made to the remote repo.
We discussed two commands that you can use to fix the error: the git pull origin [branch] and git pull --rebase origin [branch] commands.
I hope this helps you fix the error.
Happy coding!
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Sometimes, Git can’t make your change to a remote repository without losing commits. When this happens, your push is refused.
If another person has pushed to the same branch as you, Git won’t be able to push your changes:
$ git push origin main
> To https://github.com/USERNAME/REPOSITORY.git
> ! [rejected] main -> main (non-fast-forward)
> error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.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.
You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:
$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work
Or, you can simply use git pull to perform both commands at once:
$ git pull origin YOUR_BRANCH_NAME
# Grabs online updates and merges them with your local work
What is ‘failed to push some refs to’ error
failed to push some refs to is a Git error that many developers frequently face. It occurs when a developer attempts to push committed code to an external git repository. The ability to push code stopped working suddenly, despite it working yesterday or the day before. It can be a source of frustration and annoyance for many.
failed to push some refs to errors are often caused when changes are not committed before pushing, issues with Git pre-push hook, incorrect branch name, or the local repository not being in sync with the Git repository. It occurs most of the time because multiple contributors are working on the same branch and the remote repository is further along than what you currently have on your local machine.
It is easy for git pushes to overlap when working in teams or for ref heads to be in different positions. This overlap can cause the repository to be out of sync, which is why the failed to push some refs to error are so frequent.
What causes ‘failed to push some refs to’ error
When multiple developers work on the same branch, it can cause a sequencing issue in Git. A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin.
Here is an abstraction of what incompatibility looks like in Git:
A -- B -- C -- D (on the remote) A -- B -- E (on your local machine)
Based on the above, your local machine is missing commits C and D. Meanwhile, you are trying to slot in your commit – E – between B and C on the remote.
Before Git lets you proceed, you will need to integrate the remote changes into your local repository. This step will fix any incompatibility issues and ensure that your version is up to date with the remote.
How can you fix ‘failed to push some refs to’ errors
Here are the scenarios that may cause the failed to push some refs error, and how to correct them:
1.Another developer pushed a commit to the same branch
The error in your terminal looks like this:
To [email protected]:sometest.git ! [rejected] your-branch -] your-branch (non-fast-forward)
When this occurs, the head sits at different positions on the same code timeline, and Git does not know how to handle it. This is because the origin repository is ahead of where you currently are. To fix this issue, run git pull on your local repository. This should allow you to push to origin again.
git pull origin [your-branch] git push origin [your-branch]
2. You got a ‘master (non-fast-forward)’ error with a ‘failed to push some refs to’ error
A git fast-forward happens when the ref pointer gets moved forward in the commit history. However, if your code diverges before it reaches the latest commit, it can cause the non-fast-forward issue and lead to a failed to push some refs to error.
To solve this issue, you can pull with the --rebase flag. --rebase will let you move your intended files to commit over to the latest pull code.
Here is how to pull with --rebase:
git pull --rebase origin [branch]
3. You got a ‘master (fetch first)’ error with a ‘failed to push some refs to’ error
When this occurs, someone has pushed to the branch before you. Git wants you to pull first before you can push your committed changes.
To prevent the loss of your work during the pull, you can stash your local changes.
The common suggested fix is to use --force flag to push through the local changes. However, it is good practice to avoid using the --force flag as it can cause inconsistency issues. Instead, use --rebase to move the ref heads and update your local repository without causing a divergence in the remote repository.
Using --force to try and fix the failed to push some refs to error will only result in more errors in the long run. This occurs because --force uses a brute force method that puts your current code and its ref head as the source of truth.
As a result, the changes in the remote can be overwritten by what you have pushed, removing any features or updates that other developers may have committed.
Only use --force if you are comfortable with features not on your local being overwritten with what you’ve currently got. Use the --force flag if you are confident that your local repository in its current state is correct.
How to prevent ‘failed to push some refs to’ errors
To prevent failed to push some refs to errors in Git, it is good practice to avoid having multiple developers work on the same branch simultaneously. Instead, use feature branches that merge into a master branch or something equivalent.
If you get a failed to push some refs to error, the main thing to do is git pull to bring your local repo up to date with the remote. Avoid employing the --force flag when using git pull and prevent other developers’ accidental overwrites of committed features.
Use the --rebase flag instead to avoid other errors from occurring while fixing your original failed to push some refs to error.
Kubernetes Troubleshooting with Komodor
We hope that the guide above helps you better understand the troubleshooting steps you need to fix the failed to push some refs to error.
Keep in mind that this is just one of many Git errors that can pop up in your K8s logs and cause the system to fail. Due to the complex and distributed nature of k8s,
the search for the root cause of each such failure can be stressful, disorienting, and time-consuming.
This is why we created Komodor, which acts as a single source of truth (SSOT) to streamline and shorten your k8s troubleshooting processes. Among other features, it offers:
- Change intelligence: Every issue is a result of a change. Within seconds we can help you understand exactly who did what and when.
- In-depth visibility: A complete activity timeline, showing all code and config changes, deployments, alerts, code diffs, pod logs, etc. All within one pane of glass with easy drill-down options.
- Insights into service dependencies: An easy way to understand cross-service changes and visualize their ripple effects across your entire system.
- Seamless notifications: Direct integration with your existing communication channels (e.g., Slack) so you’ll have all the information you need, when you need it.

The git “error: failed to push some refs to remote repo” occurs when you try to push your local committed code to a remote repo with git push or git push -u origin main. The reason may be based on many issues related to the connection between the local branch and the remote branch and I will tell you a few. Before you can push to any remote GitHub repo, you have to make sure that there’s a connection between the remote and the local repo. It will be time saving if you do set an upstream for every branch so that you will just pull and push as you make changes to your code.
If you don’t have the required permissions, you will also not be able to push your source-code to a remote repo. This may due to the fact that you don’t own or you’re not part of the repo team/contributors. If that’s the case then what you need to do is to raise a pull request if you have already forked the repository. But if you do own the repo then it could mean that you have wrong configuration setup on your local environment.
If you’re presented with the bug failed to push some refs to remote error while pushing code to git, it could be that you did not commit your changes before pushing. Errors such as incorrect branch name, error: src refspec main does not match any and pre-receive hook declined are all related to the fact that your local repository not being in sync with the remote repo.
Also, when the remote origin is ahead of your local repo on your machine, you will not be able to push some refs to the remote repo. This may due to the fact that many contributors/team members working on the same branch. If other contributors push their changes, you have to pull and merge first before you can push so that your source code will match that of your team members. This implementation from the Git team is not really a bug but a hint to notify you that the remote origin has some file changes you do not have and it helps to avoid overlapping (meaning your local rep needs to be updated first so that you don’t override changes made by other team members.)
Below is how the error is presented…
$ git push origin main
Username for 'https://github.com': user_test
Password for 'https://github.com@github.com':
To https://github.com/justice1891/testpullpush.git
![rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/ justice1891/testpullpush.git'
How to Fix the error: failed to push some refs to Error in Git. There are several ways you can fix the above error, but let take it step-by-step.
You can use the git command git pull origin [branch] or git pull --rebase origin [branch] to fix the error: failed to push some refs to [remote repo] error. After executing the above command, the error will be resolved and you should be able to push. Let me show you how to use the command.
What the command will do is that, it will send a pull request to the remote repo and fetch the new changes and merge it with your local repo. That’s how git ensures that both the local branch and the remote origin matches, and that means you can push your changes to GitHub after merging is done successfully. In our case, we have an error so let try to send a pull request first (you can do this on your terminal if you have Git already configured, or move to your local repo directory and use Git-Bash instead)
git pull origin main
Note that in the above code, we’re sending a pull request to fetch the changes from the main branch. If you have a different branch name like master, then replace main with that branch name. If the request didn’t succeed when you execute the above command whiles syncing your local and remote repo, make sure you have git properly configured on your system or check and see if there is a connection between the repos. Then you can go ahead and push your changes using below command:
git push -u origin main
You should be able to push your code to the remote repo without encountering any bug. But if the error persists seek of other reasons and you’re presented with an error saying ‘fatal: refusing to merge unrelated histories’ then apply below solution to get it fixed.
How to fix the error “error: failed to push some refs” in Git Using git pull –rebase? If you have different number of commits in your local-branch it will be behind the remote branch and the first solution will not work for this situation. You can proceed and fix the error using the git pull –rebase command.
git pull --rebase origin main
git push -u origin main
If you’re presented with the response Successfully rebased and updated refs/heads/main on the terminal after running the first command above, then the pull is successful. Both the local and the remote repo will be sync and rebase to have the same state and code or file changes with your local branch having one commit ahead the remote origin. That means you have to run the second command above to push your local commit, and this will push it state to the remote repo.
The next solution is to rename your branch and push again if you’re able to pull but cannot push to a remote repo. Sometimes because of other related reasons, git suggest that you rename your branch before you push, if your branch name contains hyphens and other special characters. Example, if your local branch name includes hyphens, replace it with underscore with git branch -m new_branch_name before you push.
git branch -m new_branch_name
git push -u new_branch_name
Check also, how to fix error – JSX expressions must have one parent element in React
Another reason that leads to the occurrence of the above error is when you add new changes with the command git add --all and you decide to push with git push without committing the changes. Git treat every commit as a separate version of your code or files in a branch so that you can switch back and forward and modify that commit separately. Make sure to commit all your code changes before you push, if not you will be presented with message saying “Everything up-to-date” meanwhile the actual files you just added needs to be pushed to the remote origin. Below is an example of such bug and how to solve it…
git add –all
git push

You can see it says ‘Everything up-to-date’ meanwhile I was trying to push the changes I just added to the branch. If I run git status it will then list all the file changes, I just added waiting to be committed.

The point here is that, git will not push the changes you just added to the remote origin unless you commit first.
git add –-all
git commit -m “first commit”
git push
Check also, How to fix typeError: ‘builtin_function_or_method’ object is not subscriptable in python
Summary
You have seen why the git error: failed to push some refs to [remote repo] occurs and how to resolve it with commands like git pull --rebase origin [branch], git pull origin [branch] and git push -u origin main. Please note carefully the underline reasons for the occurrence of the above error. It mainly because, you have to pull some changes from GitHub to your local repository before you can push. The error will be thrown if you don’t pull first and you attempt to push (git will not allow you to do that.)
Let me know if you did encounter any difficulties whiles applying any of the above solutions.
Keep coding!
Tagged error: failed to push some refs to
