Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do i get "error: failed to push some refs"?

Tags:

git

I have a remote git repository and a local one that i work with. Whenever i do any changes locally, i push them to the remote. Then i sometime do a "git commit" on the remote one to store the changes on the remote files.

I do not edit the remote repository directly at all. I just commit the changes. And i'm a single developer, no one else works on that repos.

Why do i get an error that, from what i know, means that i have to pull first ?

I don't want to pull because the remote repos files are outdated and it will lose my local changes. This is really annoying, why does this happen ? And how can i fix without having to pull or recreate the repository ? (as you can see, this is sort of like a subversion type of version control style here)

EDIT - The error :

To ssh://...
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://...'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
like image 716
Spyros Avatar asked Apr 14 '11 20:04

Spyros


People also ask

Why is GitHub rejecting my push?

Sometimes, Git can't make your change to a remote repository without losing commits. When this happens, your push is refused. If another person has pushed to the same branch as you, Git won't be able to push your changes: $ git push origin main > To https://github.com/USERNAME/REPOSITORY.git > !

Why is git push origin not working?

If git push origin master not working , all you need to do is edit that file with your favourite editor and change the URL = setting to your new location. Assuming the new repository is correctly set up and you have your URL right, you'll easily be able to push and pull to and from your new remote location.

How do I push code to GitHub?

At the top of your repository on GitHub.com's Quick Setup page, click to copy the remote repository URL. In the Command prompt, add the URL for the remote repository where your local repository will be pushed. Push the changes in your local repository to GitHub.com.


2 Answers

I have also faced same error and wasted lots of time, Here is final solution.

  1. when use git push origin master then we get error so solution is to force push. so use second option. it worked for me.

  2. git push origin master --force

like image 195
siddhartha shankar Avatar answered Sep 23 '22 16:09

siddhartha shankar


Here is another option.

git reset --mixed origin/master
git add .
git commit -m "Your message"
git push origin master
like image 41
marquescharlon Avatar answered Sep 24 '22 16:09

marquescharlon