开发者

Git: apply topic branch using rebase (without merge)

There is a small topic branch (on a contributor's remote repo) that I'd like to apply on top of my master. I think the canonical way to do this is:

git merge contributor/topic-branch

But I'd like to have the commits applied one-by-one instead of generating a merge commit.

Naively running git rebase contributor/topic-branch obviously won't work because it applies my master onto the topic branch, as if the topic branch was my upstream. So I tried this:

git rebase master contributor/topic-branch

And this does what I want, except that now I have a detached HEAD, and I need to fix the master branch to point to the HEAD (using branch -f). I could of course write a Bash function to do this automatically, but is there a "proper" way to pull in a topic branch with开发者_如何学JAVAout using merge?


How about:

git checkout topic-branch
git rebase master
git checkout master
git merge topic-branch

This solves the problem for a local branch. Solving for a tracking branch is left as an exercise.

EDIT: I suppose I should explain what's going on here. First you change to the topic branch; then you rebase the topic branch so that it is based on master. (Then, of course, you test that everything still works. You do have automatic tests, right?) Now that the topic branch is ahead of the master, you can change back to master and merge topic into master; no merge commit is necessary, since it's a fast-forward.


I also just figured out that cherry-pick supports commit ranges, so you could do

git cherry-pick HEAD..contributor/topic-branch

(This successively applies all commits in topic-branch that are not reachable from HEAD.)

I get a sense from the man page that this will fail if there are merge commits in the topic branch, so this only works for simple cases with linear history.

Also, if you do this with a local topic branch, branch -d will not detect that topic-branch was merged, so you'll have to use -D. (Philip's rebase+merge method by comparison doesn't have this problem.)


If you want to port range of commits from commit-start-id to topic-branch-head, then do

git checkout contributor/topic-branch
git rebase --onto master <commit-start-id>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜