How to git cherrypick all changes introduced in specific branch

It seems that cherry-pick does what we want in terms of only getting changes from selected commits, but I would really like a way to cherry-pick all commits in a given branch at once, without having to look up the commit ids every time.


Using cherry-pick

git cherry-pick allows you to pick any commits you made in any branch to any other branch. In your case you can simply checkout master branch and then cherry-pick all the commits from any branch that you wish (cherry-pick supports ranges so you can specify start and end commit instead of listing all the commits).

This way you can control the way your commits will appear in the desired branch.

For example:

git cherry-pick ebe6942..905e279

# Find the range of commits you wish to re-add to your branch.
# then use cherry-pick to add them back to the branch
git cherry-pick start..end

# If you wish to include the start commit as well add the ^
# This will result in a cherry-pick of the start commit included as well 
git cherry-pick start^..end

How to find first commit of branch?

git log

 # print out the latest commit (first one) of the given branch
 git log  --oneline | tail -1

merge-base

Use the merge-base command to find where the branch was split from the original branch:

git merge-base A B

A one liner would be:

git cherry-pick $(git merge-base master my/branch)..my/branch

You can also convert this into a git alias or bash function. I prefer using it like this so I only change single place:

BRANCH=my/branch; git cherry-pick $(git merge-base master ${BRANCH})..${BRANCH}

If you want to cherry-pick all commits from branch dev.

Try:

git cherry-pick ..dev


Assuming you know the number of commits you wish to pick from the branch you can use the relative commit notation.

git cherry-pick BRANCH_A~10^..BRANCH_A

This will cherry pick all commits starting at 10 commits before (~10) BRANCH_A's HEAD, inclusive of the starting commit (^), and will get all commits in the range (..) through to BRANCH_A's HEAD.