Cherry-picking lets you apply the change another commit introduced to your current branch.
Merging with squashing (followed by committing) does the same as merging (with respect to the resulting working directory and index, not the history).
Here's an example (some output by git is not shown).
Setup
$ git init
$ echo sahred file > shared-file.txt
$ git add .
$ git commit -m 'added shared-file.txt in master branch'
$ git checkout -b dev
$ touch dev.txt
$ git add .
$ git commit -m 'added dev.txt in dev branch'
$ echo shared file > shared-file.txt
$ git add .
$ git commit -m 'fixed typo in shared-file.txt in dev branch'
$ git checkout master
We now have a branch dev
with an additional file (dev.txt
) and where the typo in shared-file.txt
is fixed. We switched back to the master
branch to compare cherry-picking and merging.
Cherry-picking
$ git cherry-pick dev
$ git log --oneline --graph
* 8019b05 (HEAD -> master) fixed typo in shared-file.txt in dev branch
* 7dbd3aa added shared-file.txt in master branch
$ ls
shared-file.txt
$ cat shared-file.txt
shared file
As you can see, after cherry-picking the change the last commit in the dev
branch introduced was applied (fixing the typo in shared-file.txt
) but the additional file dev.txt
was not carried over, because it was not created in the last commit in the dev
branch.
We now undo the cherry-pick and compare with the result of merging.
Merging (with squashing and committing)
$ git reset HEAD~1 --hard # undo cherry-pick
$ git merge dev --squash
$ git commit -m 'merged dev'
$ git log --oneline --graph
* 01dd755 (HEAD -> master) merged dev
* 7dbd3aa added shared-file.txt in master branch
$ ls
dev.txt shared-file.txt
$ cat shared-file.txt
shared file
As you can see, both the typo were fixed and dev.txt
was carried over. This is because merging with squashing (followed by committing) is the same as the regular merge of two commits with respect to the resulting working directory and index (but not the history of course).