Once again: fast

So I rebased a branch against the master. However,

git push --set-upstream origin MyBranch

I get

 ! [rejected]        MyBranch -> MyBranch (non-fast-forward)
 error: failed to push some refs to 'https://mygit@bitbucket.org/mygit/myproject.git'
 hint: Updates were rejected because the tip of your current branch is behind
 hint: its remote counterpart. Integrate the remote changes (e.g.
 hint: 'git pull ...') before pushing again.

So, ok. Rebased again. Completed without issue. Then

 git pull

 * branch            HEAD       -> FETCH_HEAD

Ok, Already up to date

git push --set-upstream origin MyBranch

Again, the error. I could force push, but I don't like the risk of losing things and to be honest, I would really like to know a proper solution for this annoying issue that seems to happen even if I play by the rulebook.


From your description, I'm going to try and make an educated guess on what might have likely happened.

Git refuses to push your rebased commits into the upstream branch ( origin/MyBranch ) because they have a different commit hash than the ones that are already there. They're different because you rebased your local commits on top of a different commit than the one that's in the upstream branch.

The problem

Here's an example of how I think your situation might look like:

      Local                                Origin
A---B---C---F (master)                A---B---C---F (master)
                                             
             D'---E' (MyBranch)                D---E (MyBranch)
  • The commits D and E in origin/MyBranch were originally based on top of C from master . Then F became the new HEAD of master .
  • When you did git pull in master you got the new commit F on your local repo.
  • You rebased MyBranch on top of master , which is now F , and that changed the commits hashes of D and E into D' and E' .
  • Issuing git push origin MyBranch refuses to overwrite the commits because Git sees them as different commits than the ones in origin , which are still based on top of C .
  • The (possible) solution

    If you're the only one working on MyBranch or if you can easily communicate with anyone that might be interested in doing work in that branch, I say just force push your rebased commits. Again, assuming nobody else has pushed commits to origin/MyBranch in the meantime, you won't lose anything.

    If this doesn't reflect your situation at all, please feel free to ignore this advice and, if possible, add more details about the problem.

    链接地址: http://www.djcxy.com/p/49250.html

    上一篇: git从上游提取更改并将它们合并到前端

    下一篇: 再一次:快