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

前端 未结 7 1444
清酒与你
清酒与你 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:45

    try this command git push -f origin master

    0 讨论(0)
  • 2021-02-07 01:47

    Be sure to verify that the pull actually worked. It is possible the merge (which is part of a pull) failed.

    When I had this error message I failed to notice the pull failed. The pull failed due to some uncommitted file changes (unrelated to the files I was trying to push) which caused the merge to fail. I reverted those file changes (because they were not important) and then did a pull again. After the successful pull, the push worked.

    0 讨论(0)
  • 2021-02-07 01:48

    Be sure your branch is unprotected, or developer can push is checked.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 02:00

    Also, the error happens when you put the wrong branch name into your push command.

    For example, say you're on a branch called iss8, but you tell git to push to iss3 (like I just did), you'll get the same error.

    $ git push origin iss3
    error: src refspec iss3 does not match any.
    error: failed to push some refs to 'git@myhost.com:myuser/myproject.git'
    

    Hmm, check status:

    $ git status 
    On branch iss8
    nothing to commit, working directory clean
    

    Oh, look, I'm on a different branch. Thanks git for not letting me do something really dumb.

    $ git push origin iss8
    Counting objects: 15, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (9/9), done.
    Writing objects: 100% (9/9), 2.62 KiB | 0 bytes/s, done.
    Total 9 (delta 3), reused 0 (delta 0)
    To git@myhost.com:myuser/myproject.git
     * [new branch]      iss8 -> iss8
    
    0 讨论(0)
  • 2021-02-07 02:01
    1) git stash 
    2) git pull
    3) git push
    

    My Original Error:

    After "git stash"

    0 讨论(0)
提交回复
热议问题