Advertisement
Advertisement


git error: failed to push some refs to remote


Question

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

2020/04/06
1
483
4/6/2020 2:10:32 PM

Accepted Answer

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 master
git push origin master

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/master (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 master

That would establish a tracking relationship between your local master 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/master:

git reset --mixed origin/master
git add .
git commit -m "This is a new commit for what I originally planned to be amended"
git push origin master

There is no need to pull --rebase.

Note: git reset --mixed origin/master can also be written git reset origin/master, since the --mixed option is the default one when using git reset.

2020/05/07
669
5/7/2020 1:53:29 PM

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.

2018/01/19

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.

2015/05/22

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.

2017/01/04

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

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

This worked for me.

2018/02/01

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
2014/11/21