You Should Really Branch Out a Bit-Part 2
19 Sep 2012
Merge, Squash, or Cherry-Pick?
When you need to share changes between branches, you need to merge but there are different options that you may want to choose based on your situation. Let's talk about those options.
Straight merging: This takes one branch and merges it into another branch. The branch that you are currently working in when you do the merge will be the branch that contains the merge after you run the command. It looks like this...
1 $> git merge the_name_of_the_branch_you_want_to_merge_into_the_branch_you_are_currently_in 2
Squashing: You use this when you want to take all the commits you have in a branch and squash them into one commit as you merge. A good time to use this is if you have only one individual change with several commits. If you have more than one change, you probably want to keep the commits separate. If you choose to squash, you would do it like this... (just like the example above, make sure that you are in the branch that you want to merge to.)
1 $> git merge -- squash the_name_of_the_branch_you_want_to_merge_into_the_branch_you_are_currently_in 2
Once you have done that you should commit those changes to your current branch.
Cherry Picking: Sometimes, you like what you did in one commit that is in a branch, but you do not want to do a full merge. Let's say that you have switched to the branch that you want to merge to. You can use the reference to a specific commit to cherry pick it.
1 $> git cherry-pick number_of_the_commit_you_want_to_cherry_pick 2
By default, you will have a new commit with the changes from the cherry picked commit. If you want to cherry pick multiple commits and not have a new commit created for each one you cherry pick, you pass the -n parameter and it will stop before it creates a commit.
1 $> git cherry-pick -n number_of_the_commit_you_want_to_cherry_pick 2
You can continue to do this until you have all the commits you want. Then you can commit them all together at the same time.