gitgit-branchbranching-and-merginggit-rebase

Git: apply feature branch using rebase (without merge)


There is a small topic branch that I'd like to apply on top of my main branch. I think the canonical way to do this is:

git merge topic-branch

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

Naively running git rebase 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 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 without using merge?


Solution

  • 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.