gerrit - git (pull vs checkout vs cherrypick) which is for what?

GitGerrit

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSandeepView Question on Stackoverflow
Solution 1 - GitAkashView Answer on Stackoverflow
Solution 2 - GitfejeseView Answer on Stackoverflow
Solution 3 - GitMattView Answer on Stackoverflow