Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does picking a range of commits with cherry-pick or rebase --onto end up with the same result?

I sometimes want to pick a range of commits from a different repository. I know two ways to do that.

1.

git checkout myBranch
git cherry-pick begin..end

or

  1. git rebase --onto myBranch begin end

I find the first version easier to remember. However, I read a lot about how cherry-picking is evil compared to merging because it kinda breaks the history. But what I haven't figured out yet is if there is a difference between cherry-picking a range of commits or rebasing them with --onto

I tend to think that there shouldn't be a difference. Am I mistaken?

like image 794
Christoph Avatar asked Jul 11 '12 08:07

Christoph


People also ask

Can you cherry pick a range of commits?

Note: as of Git 2.9. x/2.10 (Q3 2016), you can cherry-pick a range of commit directly on an orphan branch (empty head): see "How to make existing branch an orphan in git". A rebase --onto would be better, where you replay the given range of commit on top of your integration branch, as Charles Bailey described here.

Is rebase same as cherry pick?

Both do very similar things; the main conceptual difference is (in simplified terms) that: rebase moves commits from the current branch to another branch. cherry-pick copies commits from another branch to the current branch.

What happens when you cherry pick a commit?

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

Does git cherry pick rewrite history?

The git cherry-pick is a very useful command. It takes changes from a specific commit and applies them to your current branch in a new commit. As a consequence, git cherry pick does not alter your current Git history : instead it adds commits to it.


2 Answers

These two commands are equivalent, you're just doing the work that a normal rebase would do to figure out the unmerged commits to replay onto the target branch.

like image 51
Ana Betts Avatar answered Oct 21 '22 10:10

Ana Betts


Rebasing and cherry-picking are just as 'bad' for merges. They both result in forgetting the IDs of the original commits for the changes you pick up; as a result, later merges may attempt to apply the same changes more than once.

like image 24
bdonlan Avatar answered Oct 21 '22 10:10

bdonlan