开发者

when trying to update a newly created remote branch, I get "rejected"

This has happened to me several times now and I don't understand why, so I thought I'd ask... This is basically what I do:

get fetch --all
git push origin my_new_branch

Counting objects: 68, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (43/43), done.
Writing objects: 100% (43/43), 6.68 KiB, done.
Total 43 (delta 34), reused 0 (delta 0)
To git@github.com:FooBar/foobar.git
* [new branch]      my_new_branch -> my_new_branch

Then I realize.. Oops, I forgot to make sure my new branch is current with master..

git checkout master
git pull --rebase
git checkout my_new_branch
git rebase master
git push origin my_new_branch

To git@github.com:FooBar/foobar.git
 ! [rejected]        my_new_branch -> my_new_branch (non-fast-forward)
error: failed to push some refs to 'git@github.com:FooBar/foobar.git'
To prevent you from losing history, non-fa开发者_C百科st-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.

Why does this happen??? And more importantly, how can I make it not happen (other than doing -f and forcing it...) ?


You are expected to force it with -f. That's what it's there for.

Git by default refuses to push a new value of a ref that is not descendant of the previous value. That's a safety measure. If somebody else based their work on the old ref and you rewind it, you are going to cause a lot of trouble for them (well, nothing that one could not handle, but they'd need to know about it).

If you need to quickly fix up a mistake, just go ahead, rebase and force the push. If it's your own branch that you push just to show it to somebody, but nobody is supposed to base their work on top of it and everybody know they are not supposed to, just go ahead, rebase and force the push. If you want to update the branch in any other way, merge, don't rebase.


When you rebase a branch, some of the commits that constitute it are lost and new commits are re-made on top of branch on to which you are rebasing. This means that rebase doesn't preserve all of the history of a branch. When you push a branch, the head of the branch that you push must contain all of the history of the branch that you are replacing so that you don't lose any commits from the remote branch.

When you publish a branch you should avoid rebasing it. Simply merging "master" into "my_new_branch" will keep the branch up to date w.r.t master and avoid you having to rewind the remote branch.

To achieve this you can reset your "my_new_branch" with:

# (assuming no uncommitted changes that need preserving)
git reset --hard origin/my_new_branch

then merge and push:

git merge master
# resolve conflicts if needed.
git push origin my_new_branch
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜