Git - How to cherry pick a range of commits and merge into another branch

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have the following repository layout:

    • master branch (production)
    • integration
    • working

What I want to achieve is to cherry pick a range of commits from the working branch and merge it into the integration branch. I pretty new to git and I can t figure out how to exactly do this (the cherry picking of commit ranges in one operation not the merging) without messing the repository up. Any pointers or thoughts on this? Thanks!

Answers

When it comes to a range of commits, cherry-picking is was not practical.

http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git/881112#881112 http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git/881112#881112

git cherry-pick" learned to pick a range of commits
(e.g. "cherry-pick A..B" and "cherry-pick --stdin"), so did "git revert"; these do not support the nicer sequencing control "rebase [-i]" has, though.

http://stackoverflow.com/a/3933416/6309 http://stackoverflow.com/a/3933416/6309

In the "cherry-pick A..B" form, A should be older than B .
If they re the wrong order the command will silently fail .

If you want to pick the range B through D (inclusive) that would be B^..D .
http://stackoverflow.com/a/9853814/6309 http://stackoverflow.com/a/9853814/6309

http://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-into-another-branch/1994491#comment51629396_1994491 http://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-into-another-branch/1994491#comment51629396_1994491

This assumes that B is not a root commit; you ll get an "unknown revision" error otherwise.

http://stackoverflow.com/a/38285663/6309 http://stackoverflow.com/a/38285663/6309


Original answer (January 2010)

http://stackoverflow.com/questions/509859/what-is-the-best-way-to-git-patch-a-subrange-of-a-branch http://stackoverflow.com/questions/509859/what-is-the-best-way-to-git-patch-a-subrange-of-a-branch http://git-scm.com/docs/git-rebase http://git-scm.com/docs/git-rebase

If your current branch is integration:

# Checkout a new temporary branch at the current location
git checkout -b tmp

# Move the integration branch to the head of the new patchset
git branch -f integration last_SHA-1_of_working_branch_range

# Rebase the patchset onto tmp, the old location of integration
git rebase --onto tmp first_SHA-1_of_working_branch_range~1 integration

That will replay everything between:

    • after the parent of first_SHA-1_of_working_branch_range (hence the ~1): the first commit you want to replay
    • up to "integration" (which points to the last commit you want to replay, from the working branch)

to "tmp" (which points to where integration was pointing before)

If there is any conflict when one of those commits is replayed:

    • either solve it and run "git rebase --continue".
    • or skip this patch, and instead run "git rebase --skip"
    • or cancel the all thing with a "git rebase --abort" (and put back the integration branch on the tmp branch)

After that rebase --onto, integration will be back at the last commit of the integration branch (that is "tmp" branch + all the replayed commits)

http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git/881112#881112 http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git/881112#881112


http://old.nabble.com/cherry-pick---since---td10105685.html http://old.nabble.com/cherry-pick---since---td10105685.html

If you want to use a patch approach then "git format-patch|git am" and "git cherry" are your options.
Currently, git cherry-pick accepts only a single commit, but if you want to pick the range B through D that would be B^..D in git lingo, so
git rev-list --reverse --topo-order B^..D | while read rev 
do 
  git cherry-pick $rev || break 
done 

But anyway, when you need to "replay" a range of commits, the word "replay" should push you to use the "rebase" feature of Git.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-into-another-branch

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils