Git: failed to push some refs although I have done git pull

前端 未结 7 1481
清酒与你
清酒与你 2021-02-07 01:19

I started to get \'failed to push some refs\' error when I changed some files and tried to do push. Most instructions tell to do git pull first. I have done it and git says ever

7条回答
  •  渐次进展
    2021-02-07 01:59

    The best possible answer is on Git help here

    Dealing with non-fast-forward errors

    From time to time, you may encounter this error while pushing commits (git push origin master) to GitHub:

    To https://github.com/user/repo.git  
     ! [rejected]        master -> master (non-fast-forward)  
    error: failed to push some refs to 'https://github.com/user/repo.git'  
    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.
    

    This error can be a bit overwhelming at first; do not fear!

    Simply put, Git can't make the change on the remote without losing commits, so it refuses the push. Usually, this is caused by another user pushing to the same branch.

    You can remedy this by fetching and merging the remote branch:

    git fetch origin
    git merge origin master
    

    Or, you can simply use git pull to perform both commands at once:

    git pull origin master
    

    In some cases, this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don't force-push.

提交回复
热议问题