This article discuss about git checkout vs git cherry-pick command, and when to use these commands.

git checkout command switches between branches or restores working tree files. This command operates upon three distinct entities i.e. files, commits, and branches. Checking out branches is similar to checking out old commits. Files in that the local working directory is updated to match the selected branch/revision.

git checkout just takes HEAD to a commit you specify, thus making working directory exactly as it was at that commit.

git fetch you update your remote references, ie. refresh what is present in remote repository. Now git cherry-pick fetches the commit and plays it on top of the current local branch. Thus creating an entirely new commit which happens to have same changes as the one it fetched.

Most significant difference is that git-cherry-pick changes the history of a branch, while git checkout is being used only to jump to a specific commit in the branch’s history, without changing it.

git checkout is used to traverse along your branch history, pointing the HEAD variable to a specific commit. It is also used to switch between branches. git cherry-pick on the other way is used to place a single commit from another branch on top of your branch history.

Example

For example, says you have the following branches:

master:
-------
commit1 1ae4d13257425e6a0d67f35fa13e9d76c4e6987b
Date:   Thu Feb 1 15:59:15 2022 +0200


commit2 cbc7776d5542f59e1e6c7c8a22add729b
Date:   Thu Feb 1 15:44:41 2022 +0200

dev:
-------
commit1 c591438ff5abbcebcbf9c06f47de7aa840
Date:   Thu Feb 1 15:45:24 2022 +0200

You can switch into above branch dev using:

git checkout branch

and from branch you can execute the following:

git cherry-pick 1ae4d132

to play commit1 from master on top of commit1 from branch dev, making it:

dev:
-------
commit1 1ae4d13257425e6a0d67f35fa13e9d76c4e6987b
Date:   Thu Feb 1 15:59:15 2022 +0200

commit1 c591438ff5abbcebcbf9c06f47de7aa840
Date:   Thu Feb 1 15:45:24 2022 +0200

You can use traverse history in branch dev and see a single state of it using checkout

git checkout c591438ff5

This will points HEAD to commit c591438ff5, showing you the state of the branch before your cherry-picking (but it won’t change the history)