git push rejected

后端 未结 8 2021
無奈伤痛
無奈伤痛 2020-12-22 23:50

I give up! Whenever I try to push I get a stupid:

! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to \'git@github         


        
相关标签:
8条回答
  • 2020-12-23 00:07

    First, attempt to pull from the same refspec that you are trying to push to.

    If this does not work, you can force a git push by using git push -f <repo> <refspec>, but use caution: this method can cause references to be deleted on the remote repository.

    0 讨论(0)
  • 2020-12-23 00:08

    First use

    git pull https://github.com/username/repository master
    

    and then try

    git push -u origin master
    
    0 讨论(0)
  • 2020-12-23 00:09

    Is your repository at "upstream" a bare repository? I got the same error, but when I change to bare they no longer happen.

    0 讨论(0)
  • 2020-12-23 00:12

    If nothing works, try:

    git pull --allow-unrelated-histories <repo> <branch>
    

    then do:

    git push --set-upstream origin master
    
    0 讨论(0)
  • 2020-12-23 00:17

    When doing a push, try specifying the refspec for the upstream master:

    git push upstream upstreammaster:master
    
    0 讨论(0)
  • 2020-12-23 00:21

    Jarret Hardie is correct. Or, first merge your changes back into master and then try the push. By default, git push pushes all branches that have names that match on the remote -- and no others. So those are your two choices -- either specify it explicitly like Jarret said or merge back to a common branch and then push.

    There's been talk about this on the Git mail list and it's clear that this behavior is not about to change anytime soon -- many developers rely on this behavior in their workflows.

    Edit/Clarification

    Assuming your upstreammaster branch is ready to push then you could do this:

    1. Pull in any changes from the upstream.

      $ git pull upstream master

    2. Switch to my local master branch

      $ git checkout master

    3. Merge changes in from upstreammaster

      $ git merge upstreammaster

    4. Push my changes up

      $ git push upstream

    Another thing that you may want to do before pushing is to rebase your changes against upstream/master so that your commits are all together. You can either do that as a separate step between #1 and #2 above (git rebase upstream/master) or you can do it as part of your pull (git pull --rebase upstream master)

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