[git] git error: failed to push some refs to remote

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 "local" with an arrow points to the css-folder. Below, screenshot with heading "github", and a css-folder and index.php-file

This question is related to git github

The answer is


This happens to me as I had a tag with the same as the branch and I tried to push the branch to remote.


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

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.


I created an empty repo 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

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 execute the wrong sequence first, I just execute the below commands

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

I find the solution to this problem in github help.

You can see it from:Dealing with non-fast-forward errors

It says:

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

In my case closing of editor (visual studio code) solved a problem.


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

it was test/testing_routes and needed to change it to testing_route which the / is not allowed by remote.

You should ensure that the branch name format is correct.


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

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

Well if none of the above answers are working and if you have messed up something with ssh-add lately. Try

ssh-add -D

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 incorrectly and you want to remove the local repository use following steps

Remove the local repo from 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

I faced this issue but after 2 days I got a solution. Just run these two commands if you are deploying your site on GitHub pages first time.

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

For sourcetree users

First do an initial commit or make sure you have no uncommited changes, then at the side of sourcetree there is a "REMOTES", right-click on it, and then click 'Push to origin'. There you go.


Because maybe have nothing to push (really, no thing to push). Do 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 url in your case. Command git remote -v you can skip, just for checking.


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

git add .

git commit -m "your msg"


Not commiting initial changes before pushing also causes the problem


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


do these

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

Happy coding!


In my case, it was my husky package that disallows 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.


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

git push origin main

This is a common mistake, beginners can make.

Github Article


Remember to commit your changes before pushing to Github repo. This might fix your problem.


In my case 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 branch solved my problem.


I had faced same problem,fixed with 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

    (Above url https://[email protected]/User_name/sample.git refers to your bit bucket project url )

  5. git push -u origin master

hint

check if your git hub account link with your local git by using:

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

Try this git command,

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

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.

Hope this answer helps! Goodluck!


I was pushing existing typo 'evelop' branch which I did not have checkout yet, and instead, I wanted to push a branch called 'envelope'. So the branch must be existing/checkout out at local working copy in order to push of course, therefore that error, not to make a typo.


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.


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

This is probably not a common problem, but it happened to me, so I'll add it as a comment here in case someone else makes the mistake I did.

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.


Creating a new branch solved for me:

git checkout -b <nameOfNewBranch>

As expected no need to merge since previous branch was fully contained in the new one.


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 ...


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.


  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


You need to give some force

Just do push --force.


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

Once i did git commit -m "your msg" and then everything worked fine.


git push -f origin master.

this one is write


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

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

This worked for me.


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


Using a Git repo in Azure DevOps, the problem was a branch policy requiring that all changes to the branch must be made via pull request. 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.


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.


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


This issue comes when 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 latest code from remote server to local and then push

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

    >git push --force
    

    if #1 will not work then use #2 option

    Use below command to get all the options that is related to push

    > git push --help
    

Did anyone try:

git push -f origin master

That should solve the problem.

EDIT: Based on @Mehdi ‘s comment below I need to clarify something 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 better solution.


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.