[The following assumes
git pull --rebase origin main
git push origin main
1 itself is not down, as eri0o points out in : see
git pull --rebase origin main
git push origin main
2 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 --rebase origin main
git push origin main
3 would be enough. [Note: with Git 2.27 Q2 2020, a
git pull --rebase origin main
git push origin main
4 is also available for your regular pull, without rebase]
That way, you would replay [the
git pull --rebase origin main
git push origin main
5 part] your local commits on top of the newly updated
git pull --rebase origin main
git push origin main
6 [or
git pull --rebase origin main
git push origin main
7:
git pull --rebase origin main
git push origin main
8].
See a more complete example in the 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
git pull --rebase origin main
git push origin main
9, a simple
git config --global pull.rebase true
git config --global rebase.autoStash true
0 even for the first one would do the same [I.e: establishing a tracking relationship between your local main
branch and its upstream branch
git pull --rebase origin main
git push origin main
6].
See "Why do I need to explicitly push a new branch?".
Since the OP already reset and redone its commit on top of
git pull --rebase origin main
git push origin main
6:
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
git config --global pull.rebase true
git config --global rebase.autoStash true
4.
Note:
git config --global pull.rebase true
git config --global rebase.autoStash true
5 can also be written
git config --global pull.rebase true
git config --global rebase.autoStash true
6, since the
git config --global pull.rebase true
git config --global rebase.autoStash true
7 option is the default one when using
git config --global pull.rebase true
git config --global rebase.autoStash true
8.
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
git push -u origin main
0 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:
git push -u origin main
1. 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 push -u origin main
3
The
git push -u origin main
3 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:
git push -u origin main
5.
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