Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between checkout and cherry-pick?

I have fetched a particular bug fix from a remote repository. Now I have two options:

  1. git checkout FETCH_HEAD
  2. git cherry-pick FETCH_HEAD

and I am not sure what is happening in both cases. I have tried the first version and it looks to me that in this way I got the changes from the remote repository into my local repository but my local changes seem to be lost (I do not see them in the code). The second option, however, seems to do what I need (I see in the code both, my changes and changes from the remote repository).

Although it looks like I got what I needed, I still want to better understand what happen in both cases.

I have seen this question and answers but I do not understand the answer because I do not know what the following phrases mean:

  1. "working directory reflects X"
  2. "X fetches Y"
like image 201
Roman Avatar asked Jan 29 '23 17:01

Roman


2 Answers

You are right - at the first command you just switched to the remote changes, while at the last you applied the remote changes on your current branch.

Besides being used for different tasks, the 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 point in time (a specific commit) in the branch's history, without changing it.

Switch branches or restore working tree files

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

Apply the changes introduced by some existing commits

git-cherrypick on the other way is used to place a single commit from another branch on top of your branch history.

For example, says you have the following branches:

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


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

branch:
-------
commit1 c591438ff5abbcebcbf9c06f47de7aa840
Date:   Thu Feb 1 15:45:24 2018 +0200

You can switch into branch branch 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, making it:

branch:
-------
commit1 1ae4d13257425e6a0d67f35fa13e9d76c4e6987b
Date:   Thu Feb 1 15:59:15 2018 +0200

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

You can use traverse your history 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)

like image 127
Shmulik Klein Avatar answered Feb 05 '23 16:02

Shmulik Klein


cherry-pick allows you to take one commit from the top of other branch (or from the the history of other branch) and apply it on top of you current branch.

checkout - in your case, it totally replace your branch with branch from remote repository. UPD. Actually your previous branch will still exist in your local git storage, but after checkout you current branch, and all your files in working directory will be replaced with that branch on what you checkout.

like image 22
anber Avatar answered Feb 05 '23 16:02

anber