git error: failed to push some refs to

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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

What my working directory and remote repository looks like:

回答1:

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 

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:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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 


回答6:

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.



回答7:

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



回答8:

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



回答9:

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

ssh-add -D 


回答10:

In my case, this error happened because there was some maintenance happening on our version of GitLab.



回答11:

You can also fix it by editing the git config file of your project:

Where is it?

.git/config

Add the following lines at the end if they are not.

[branch "master"] remote = origin merge = refs/heads/master

This solved my problem



回答12:

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



回答13:

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

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

This worked for me.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!