8

Usually I work on the master branch, and I make some commits, and push it.

Then I also need to push these commits to other branch.

So usually, I will do:

$ git checkout another-branch
$ git cherry-pick commit1
$ git cherry-pick commit2
...
$ git cherry-pick commitn
$ git push

Some kind of stupid, is there anyway I can merge some commits from the head of the master branch so I need not bother to cherry-pick one by one.

3 Answers 3

12

It sounds like you might want to make those commits on a branch other than master, and then merge that branch to both master and your second branch:

git checkout working-branch
<do some work>
git commit
git checkout master
git merge working-branch
git checkout second-branch
git merge working-branch

This is much, much better than cherry-picking because it doesn't involve duplicating commits in the history, gets rid of any issues about cherry-picking a commit twice (which you currently must manually avoid)...and is just the way git is designed to work. I don't know what your second branch is, but what I'm describing is essentially the common workflow of periodically merging maintenance and topic branches back into master as well as any other appropriate modified-release or maintenance branches.

I strongly recommend that you adopt a workflow where this is done by merging as I described above, but to answer the question you asked, if you absolutely must work on master and cherry-pick, you might want to write yourself a little script, something like:

#!/bin/bash
# take two arguments:
# 1. other branch to put commits on
# 2. number of commits to cherry-pick from master    

if ! git checkout $1; then
    exit
fi
git rev-list --reverse -n $2 master |
while read commit; do
    if ! git cherry-pick $commit; then
        exit
    fi
done

Obviously there are ways to make the script more robust, e.g. adding the ability to resume after cherry-picks whose patches don't apply properly, but it's a start.

You can mess around with the way you use git-rev-list to select the commits, of course. You could even pass all but the first argument along to git-rev-list, so that you could do cherries-pick <branch> -n 5 master or cherries-pick <branch> release_tag..master or whatever you want. Have a look at its man page!

You can also use git-rebase as was suggested elsewhere, but because you don't actually want to move master, you'll end up doing something like this:

git branch master-copy master
git rebase --onto <branch> master~5 master
git checkout <branch>
git merge master-copy
git branch -d master-copy
0
1

git cherry-pick commit1..commitn

0

If you just want to update your branch with master, do:

git checkout branch;  git merge master
git rebase origin

If you don't want to pull everything from master, you can selectively diff revisions (boo) or use branches for individual features -- then you can just merge the feature branch into both master and your other branch.

2
  • I just want the n commits from head in master to be merged, not other commits that differ between the branch and master.
    – Sam Liao
    Commented Aug 31, 2009 at 7:13
  • 1
    git rebase -i origin; it opens a text editor for you with one commit in one line, you need to remove the lines with the commits you do not want Commented Aug 31, 2009 at 8:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.