git error: failed to push some refs to remote

GitGithub

Git Problem Overview


For some reason, I can't push now, whereas I could do it yesterday. Maybe I messed up with configs or something.

This is what happens:

When I use the git push origin master

gitbashscr

What my working directory and remote repository looks like:

Screenshot of Windows file folder with these directories: .git, css, js. And these files: index.php, readme, setsu.php. The word

Git Solutions


Solution 1 - Git

(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

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

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.

Solution 2 - Git

Try:

git push -f origin master

That should solve the problem.

Based on @Mehdi‘s comment, a clarification about —force pushing: The Git command above works safely only for the first commit. If there were already commits, pull requests or branches in previous, this resets all of it and set it from zero. If so, please refer @VonC‘s detailed answer for a better solution.

Solution 3 - Git

If you just used git init and have added your files with git add . or something similar and have added your remote branch it might be that you just haven't committed (git commit -m 'commit message') anything locally to push to the remote... I just had this error and that was my issue.

Solution 4 - Git

I had the same problem. I was getting this problem because I had not made any commits, not even an initial commit and still I was trying to push.

Once I did git commit -m "your msg", everything worked fine.

Solution 5 - Git

Rename your branch and then push, e.g.:

git branch -m new-name
git push -u new-name

This worked for me.

Solution 6 - Git

It has worked for me with this combination of several command lines:

git reset 
git remote -v
git pull --rebase
git init
git add -A
git commit -m "Add your commit"
git branch -M main
git push origin main --force

Be careful. If they have a Readme file, the git reset deletes them.

Solution 7 - Git

  1. git init

  2. git remote add origin https://gitlab.com/crew-chief-systems/bot

  3. git remote -v (for checking current repository)

  4. git add -A(add all files)

  5. git commit -m 'Added my project'

  6. git pull --rebase origin master

  7. git push origin master

Solution 8 - Git

I found the solution to this problem in GitHub help (Dealing with non-fast-forward errors):

> 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 branch > # Merges updates made online with your local work > > Or, you can simply use git pull to perform both commands at once: > > $ git pull origin branch > # Grabs online updates and merges them with your local work

Solution 9 - Git

I followed the following steps and it worked for me.

 rm -rf .git
 git init
 git add .
 git commit -m"first message"
 git remote add origin "LINK"
 git push -u origin master
 

Solution 10 - Git

I had faced the same problem and fixed it with the below steps.

  1. git init

  2. git add .

  3. git commit -m 'Add your commit message'

  4. git remote add origin https://[email protected]/User_name/sample.git

    (The above URL, https://User_[email protected]/User_name/sample.git, refers to your Bitbucket project URL)

  5. git push -u origin master

Hint

Check if your GitHub account links with your local Git repository by using:

git config --global user.email "[email protected]"
git config --global user.name "Your Name"

Solution 11 - Git

If you were using git push origin master, change it to git push origin main and vice versa.

Solution 12 - Git

I created an empty repository in GitHub and have my code locally. I faced the same issue now, as I followed the below sequence,

git init
git commit -m 'Initial Commit'
git remote add origin https://github.com/kavinraju/Repo-Name.git
git add .
git push -u origin master

The issue was: I tried to commit before staging the files I have.

So we need to stage the files and then commit.

This is the correct sequence.

git init
git add .
git commit -m 'Initial Commit'
git remote add origin https://github.com/kavinraju/Repo-Name.git
git push -u origin master

Since I executed the wrong sequence first, I just executed the below commands:

git add .
git commit -m 'Initial Commit'
git push -u origin master

Solution 13 - Git

Because maybe it has nothing to push (really, nothing to push). Do it like this:

git remote add origin https://github.com/donhuvy/accounting133.git
git remote -v
git add .
git commit -m"upload"
git push --set-upstream origin master

Change the remote repository's URL in your case. You can skip command git remote -v, just for checking.

Solution 14 - Git

If you are using Gerrit, this could be caused by an inappropriate Change-id in the commit. Try deleting the Change-Id and see what happens.

Solution 15 - Git

Remember to commit your changes before pushing to the GitHub repository. This might fix your problem.

Solution 16 - Git

Not committing initial changes before pushing also causes the problem.

Solution 17 - Git

Use:

git push origin {your_local_branch}:{your_remote_branch}

If your local branch and remote branch share the same name, then can you omit your local branch name. Just use git push {your_remote_branch}. Otherwise it will throw this error.

Solution 18 - Git

GitHub changed the default branch name from master to main. So if you created the repo recently, try pushing the main branch.

git push origin main

This is a common mistake beginners can make.

GitHub article Renaming the default branch from master.

Solution 19 - Git

Before push, you have to add and commit the changes or do git push -f origin master.

Solution 20 - Git

Try this Git command,

git push origin master –f
git push origin master --force

Solution 21 - Git

Using a Git repository in Azure DevOps, the problem was a branch policy requiring that all changes to the branch must be made via a pull request (PR). Trying to push changes directly to the branch generated the error "failed to push some refs to ...".

I created a PR branch and pushed without problem.

Solution 22 - Git

Just run these two commands if you are deploying your site on GitHub pages for the first time.

git commit -m "initial commit"
git push origin +HEAD

Solution 23 - Git

In my case there was a problem with a Git pre-push hook.

Run git push --verbose to see if there are any errors.

Double check your Git hooks in the directory .git/hooks or move them temporarily to another place and see if everything works after that.

Solution 24 - Git

Due to the recent "replacing master with main in GitHub" action, you may notice that there is a refs/heads/main if you do git show-ref. As a result, the following command may change from

git push heroku master

to

git push heroku main

That will solve your issue.

Solution 25 - Git

In my case, it was my husky package that disallowed the push.

> husky - pre-push hook failed (add --no-verify to bypass)
> husky - to debug, use 'npm run prepush'
error: failed to push some refs to 'https://[email protected]/username/my-api.git'

> To push it forcefully, just run git push origin master --no-verify

I ran npm run prepush to see debug the error, and this was the cause:

npm ERR! code ELOCKVERIFY
npm ERR! Errors were found in your npm-shrinkwrap.json, run  npm install  to fix them.
npm ERR!     Invalid: lock file's [email protected] does not satisfy loopback-utils@^0.9.0

Ran npm install and commit it, and the problem is fixed.

Solution 26 - Git

The fact that GitHub changed master to main made me encounter this issue. So from now on, the solution to push to origin is:

git push -u origin main

Solution 27 - Git

These steps worked for me:

  1. Switch to current branch & pull latest code

  2. Rename local branch

    git branch -m [new-name]

  3. Push local branch to server

    git push origin [new-name]

  4. Remove branch from server

    git push origin --delete [old-name]

Solution 28 - Git

Creating a new branch solved it for me:

git checkout -b <nameOfNewBranch>

As expected, there isn’t any need to merge since the previous branch was fully contained in the new one.

Solution 29 - Git

It may happen when you don't have any files. Try to create a text file, and then follow the following commands:

git add .
git commit -m "first commit"
git push --set-upstream origin master

Solution 30 - Git

For me the problem was I did not add the files before the commit.

git add .

git commit -m "your msg"

Solution 31 - Git

Do these:

git rm --cached *
git add .
git commit -m"upload"
git push --set-upstream origin master

Solution 32 - Git

In my case, the branch name prefix was already present at remote, so basically if you have a branch name 'fix' you cannot push another branch with name 'fix/new_branch_name'.

Renaming the branch solved my problem.

Solution 33 - Git

Best use rm -rf .git/hooks and then try git push

Solution 34 - Git

I created a custom pre-push file, and I forgot to end it with exit 0.

That caused me to get this "failed to push some refs" error. I added exit 0 to the end of my pre-push hook and, of course, it works fine now.

Solution 35 - Git

Use

git push -f origin master

This one is write.

Solution 36 - Git

I am not sure if this applies, but the fix for me was to commit something locally after git init. Then I pushed to remote using --set-upstream.

Solution 37 - Git

If you are attempting to initialize a directory with an existing GitHub repository, you should ensure you are committing changes.

Try creating a file:

touch initial
git add initial
git commit -m "initial commit"
git push -u origin master

That will place a file named initial that you can delete later.

Solution 38 - Git

Unfortunately, I could not solve the problem with the other solutions, but my problem was that the branch name I wanted to push was not accepted by remote. I changed it to the correct format, and it was accepted.

It was test/testing_routes, and I needed to change it to testing_route in which the forward slash (/) is not allowed by remote.

You should ensure that the branch name format is correct.

Solution 39 - Git

This issue comes when the remote server has some extra commit which is not available in your working directory. Below is the solution to fix this issue.

  1. To get the latest code from the remote server to local and then push, do

    git pull
    git push
    
  2. Directly do the force push to the remote server.

    git push --force
    

If #1 will not work, then use the #2 option.

Use the below command to get all the options that are related to push:

git push --help

Solution 40 - Git

In my case I misspelled the name of the branch. Locally I did something like:

git push --set-upstream origin feture/my-feature

where my branch name was missing the a in feature. I corrected it to:

git push --set-upstream origin feature/my-feature

And everything worked fine.

Solution 41 - Git

In our case, retrying to push solved the problem. Probably a network slowness caused the issue.

Solution 42 - Git

In my case, I missed amending. I just needed to run git commit --amend and then push. It fixed the issue. It might help someone who has previously committed code.

Solution 43 - Git

You will also get this error if you created an empty repo and forgot to use

git init

first before pushing your first commit.

Solution 44 - Git

I tried the 'git push origin main' and then got the message. Then I tried 'git push' alone, but it was not working.

I checked if I had committed to be sure (yes). I tried the 'pull' and then 'push' again, but nope.

And before starting some stunts, I just closed and opened a new terminal and then 'push' again and it worked :p

Solution 45 - Git

Check if your Internet connection is working fine and has got good speed.

I was trying to push with my 4G mobile hotspot and getting this error for nearly 10 minutes.

P.S. Here in India, we get 3G speed for a 4G network, so before doing something fancy; just see if there is reasonable speed available :)

Solution 46 - Git

Well, if none of the previous answers are working and if you have messed up something with ssh-add lately, try

ssh-add -D

Solution 47 - Git

You need to give some force

Just do push --force.

Solution 48 - Git

For Sourcetree users

First do an initial commit or make sure you don't have any uncommitted changes. Then at the side of Sourcetree there is a "REMOTES". Right-click on it, and then click 'Push to origin'. There you go.

Solution 49 - Git

In my case the problem was that (strangely) there was no branch called master. I took the repository from GitHub.

Solution 50 - Git

I was pushing an existing branch with a typo, 'evelop', which I did not have checked out yet, and instead, I wanted to push a branch called 'envelope'.

So the branch must exist and checked out at the local working copy in order to be able to be pushed, of course. Therefore the solution to that error is to not make a typo.

Solution 51 - Git

I have also faced this issue when using the command.

git push -u origin main

So I cleared all cache of npm using npm cache clean --force and tried to push again. That's worked for me.

Solution 52 - Git

For me, I forgot to add and commit before I hit push.

So:

git add --all
git commit -m "First commit."

Then push it, you're good to go :)

Solution 53 - Git

git error: failed to push some refs to also comes when the local repository name does match with the corresponding remote repository name. Make sure you are working on the correct pair of repository before you pull changes to remote repository.

In case you spell it incorrectly and you want to remove the local repository, use the following steps.

Remove the local repository on Windows:

  1. del /F /S /Q /A .git
  2. rmdir .git
  3. Correct the local folder name (XXXX02->XXXX20) or if it is a newly created repo delete it and recreate the repo (XXXX02 Repo name changed to XXXX20).
  4. git init
  5. Remap with remote repo if it is not mapped.
  6. git remote add origin https://github.com/<username>/XXXX20.git
  7. git push -u origin master

Solution 54 - Git

Getting an error on git push -u origin main? Try this solution. It will work 100%.

> git push origin master, change it to git push origin main

How to change Main?

> git branch -M main

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleipzyView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitcagcakView Answer on Stackoverflow
Solution 3 - GitironcladmvtmView Answer on Stackoverflow
Solution 4 - GitpramodpxiView Answer on Stackoverflow
Solution 5 - Gitp8ulView Answer on Stackoverflow
Solution 6 - GitlordView Answer on Stackoverflow
Solution 7 - GitJames SivaView Answer on Stackoverflow
Solution 8 - GitSealterView Answer on Stackoverflow
Solution 9 - GitASHISH RView Answer on Stackoverflow
Solution 10 - GitChituView Answer on Stackoverflow
Solution 11 - GitAjeigbe OladayoView Answer on Stackoverflow
Solution 12 - GitKavin Raju SView Answer on Stackoverflow
Solution 13 - GitJames GrahamView Answer on Stackoverflow
Solution 14 - GitJim SimeView Answer on Stackoverflow
Solution 15 - GitAlf MohView Answer on Stackoverflow
Solution 16 - GitClawView Answer on Stackoverflow
Solution 17 - GitYitong FengView Answer on Stackoverflow
Solution 18 - GitshivampipView Answer on Stackoverflow
Solution 19 - GitKinnera ReddyView Answer on Stackoverflow
Solution 20 - GitZin Myo SweView Answer on Stackoverflow
Solution 21 - GitMarkusView Answer on Stackoverflow
Solution 22 - GitazeemView Answer on Stackoverflow
Solution 23 - GitJochen HolzerView Answer on Stackoverflow
Solution 24 - GitLOTUSMSView Answer on Stackoverflow
Solution 25 - GitJee MokView Answer on Stackoverflow
Solution 26 - GitRoland LariotteView Answer on Stackoverflow
Solution 27 - GitTrần Thanh PhongView Answer on Stackoverflow
Solution 28 - GitSantiago M. QuinteroView Answer on Stackoverflow
Solution 29 - Gitsushil sutharView Answer on Stackoverflow
Solution 30 - GitRohitsamView Answer on Stackoverflow
Solution 31 - GitJames GrahamView Answer on Stackoverflow
Solution 32 - Gitshashwat srivastavaView Answer on Stackoverflow
Solution 33 - GitBinod SinghView Answer on Stackoverflow
Solution 34 - GitMikeView Answer on Stackoverflow
Solution 35 - GitEngr Qamar AbbasView Answer on Stackoverflow
Solution 36 - Gituser1889992View Answer on Stackoverflow
Solution 37 - GitAndreas BiggerView Answer on Stackoverflow
Solution 38 - GitMesut GUNESView Answer on Stackoverflow
Solution 39 - GitSheo Dayal SinghView Answer on Stackoverflow
Solution 40 - GitP.Brian.MackeyView Answer on Stackoverflow
Solution 41 - GitOnat KorucuView Answer on Stackoverflow
Solution 42 - GitAshvini MauryaView Answer on Stackoverflow
Solution 43 - GitMatthias KrämerView Answer on Stackoverflow
Solution 44 - GitJeremView Answer on Stackoverflow
Solution 45 - GitvikramviView Answer on Stackoverflow
Solution 46 - GitAbhisekView Answer on Stackoverflow
Solution 47 - GitAnkitView Answer on Stackoverflow
Solution 48 - GitBowie ChangView Answer on Stackoverflow
Solution 49 - GitD063520View Answer on Stackoverflow
Solution 50 - GitFantomX1View Answer on Stackoverflow
Solution 51 - GitnurmdrafiView Answer on Stackoverflow
Solution 52 - GitEissawebView Answer on Stackoverflow
Solution 53 - GitSunil Vishnu RaskarView Answer on Stackoverflow
Solution 54 - GitRizwanView Answer on Stackoverflow