60

In Android's gerrit ex: link, to download the patch, I see 4 options.

  1. repo download
  2. checkout
  3. pull
  4. cherry-pick

What is the difference between them?

Here is what I think of them. Please clarify

  1. repo download --> Downloads full source code(of all git repos in the project) till this commit
  2. checkout --> Not sure what it is.
  3. pull --> Not sure what it does?
  4. cherry-pick --> It tries to download only this change and merge it into the source code.

I know that pull and checkout are different from cherry-pick. But how are they different?

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
Sandeep
  • 18,356
  • 16
  • 68
  • 108
  • 5
    Note to would-be answerers: this question is about Gerrit, not Git. If you post an answer explaining what these terms mean in Git, you are not answering this question. – Ian Ni-Lewis May 02 '16 at 21:01

3 Answers3

60

You are right about the first one. Here are the rest of them:

  1. Checkout: Fetches the latest changes. You should already have this repo downloaded. It does not merge those new changes but makes your working directory reflect them. And a name-less commit is created to include these changes in your working directory. And a detached HEAD pointing to it. You can merge them at your leisure later.

  2. Pull: Fetches the changes AND merges them into the local branch of the same name.

  3. 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.

They are a little different than what they actually mean in git. checkout and cherry-pick don't automatically fetch the changes. checkout just takes HEAD to a commit you specify, thus making working directory exactly as it was at that commit. Likewise, cherry-pick is used to replay commits which you already have local access to.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
Akash
  • 5,153
  • 3
  • 26
  • 42
  • 4
    1. Checkout doesn't just _fetch_ the patch set, it also _checks out_ the exact commit of the patch set. 2. The patch set's commit is merged to the current head. Branch names have nothing to do with things. – Magnus Bäck Oct 21 '14 at 05:51
  • 3
    Checkout does NOT fetch. – corecursion Jun 05 '18 at 18:31
  • `check out` create the exact same commit history as the change author. i.e. If the author's change is based on commit X. Your `check out` will go back to `commit X` and base the change on it. `cherry pick` just applies the change to your current branch. So it's a bit different. – smwikipedia Aug 06 '21 at 08:12
  • what will happen if checked-out commits conflict with my working directory? – Ted Dec 13 '22 at 06:01
  • @Ted In that case, you'll need to merge the changes manually. For example, if you did a `git pull origin master` to merge upstream changes to your local master, and there are conflicts, then do a `git merge origin/master`. Then it's the normal resolution flow. – Akash Dec 28 '22 at 05:29
6

With git you have your own version of the repository. That you synchronise with others' repositories. With fetch you update your remote references, ie. refresh what others got. With checkout you switch to a specific revision. You want to do this, if you just started using this.

Now if you are already following a remote branch, you might only need to update your local branch. That's what pull does for you. It applies all the changes in the remote branch to your local one. You need this if you're using it already you just want to update.

cherry-pick let you pick one change from anywhere in the repository and will apply it on your local branch. It is handy if you're on a different branch for any reason but still need that specific change. Be aware that if you cherry-pick without pushing that change that this change is not persistent. It's committed to your local repository but not to the remote (it might be what you need in cases though).

See more about git basics for example here

fejese
  • 4,601
  • 4
  • 29
  • 36
3

checkout: you want to use it when you are depending on a particular CHANGE in a branch. say your colleague has checked in some APIs for you to consume, and you can checkout that CHANGE to a new local branch and start working on your change.

Cherrypick: you want to apply a particular CHANGE to your local branch or a particular release branch, then you cherrypick. Imagine you have a patch fix in your 1.1 release, and you want to apply that fix/CHANGE to your 2.0 branch, you can simply cherrypick it. It will create a new CHANGE in your 2.0 branch containing the fix.

here is a graphical representation: http://think-like-a-git.net/sections/rebase-from-the-ground-up/cherry-picking-explained.html

Matt
  • 445
  • 4
  • 3