Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge head commits from another branch?

Tags:

git

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.

like image 393
Sam Liao Avatar asked Aug 31 '09 06:08

Sam Liao


People also ask

Can I merge specific commit from another branch?

The git merge command is targeted at combining two branches. You can also use it for merging several commits into a single history. The merge commits involve two parent commits. Every time a new merge commit is made, git runs an automate merging of different histories.

How do I merge a branch from another branch?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.


2 Answers

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
like image 154
Cascabel Avatar answered Sep 24 '22 16:09

Cascabel


git cherry-pick commit1..commitn

like image 41
Max Nanasy Avatar answered Sep 20 '22 16:09

Max Nanasy