Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding git cherry-pick

Tags:

git

Coming from an svn background: I hardly ever branched, due to the (lack of) speed of switching and the hour or more it took to merge branches back into the trunk. Sometimes, if I needed to hotfix a problem on a web site, I'd make the change in the trunk (which would live along with previous changes or new features) and then go to that file and just do "svn up path/to/filename" and it would update only that file, fixing the problem but sparing the rest of the files.

Conceptually, this doesn't seem possible in git (or necessary); is it structured staging and grouped commits that allow for cherry-picking? So, I might change a specific area of the site and commit it as a group instead of doing how I work with svn, and go about a day's work and touch files all over the commit the whole batch at once?

like image 706
Hans Avatar asked Aug 05 '10 22:08

Hans


People also ask

What is the Git cherry pick command?

The git cherry-pick command: what it is and how to use it. With the "cherry-pick" command, Git allows you to integrate selected, individual commits from any branch into your current HEAD branch. Contrast this with the way commit integration normally works in Git: when performing a Merge or Rebase, all commits from one branch are integrated.

Is Git cherry-pick a good idea?

git cherry-pick is a useful tool but not always a best practice. Cherry picking can cause duplicate commits and many scenarios where cherry picking would work, traditional merges are preferred instead.

When to use Git cherry pick instead of merge?

This could also be useful whenever a full branch merge is not possible due to incompatible versions in the various branches. Also, you can use git cherry-pick to pull the changes introduced to a sub-branch, without changing the branch, by your colleague working on the same project.

What does the cherry pick option do?

This option overrides that behavior, allowing commits with empty messages to be cherry picked. If a commit being cherry picked duplicates a commit already in the current history, it will become empty. By default these redundant commits cause cherry-pick to stop so the user can examine the commit.


1 Answers

What you probably want to do in this case is make a new branch for your hotfix, branching off of the common ancestor of all branches which will need the fix. For example, suppose you have a couple maintained releases along with your current development:

- X - o - o - o - o - o - o - o - o - o (master)
   \                       \
    o - o - o (release A)   o - o (release B)

If you need to make a hotfix which will apply to both releases, create a branch starting from the commit labeled X. Commit your fix, then merge that branch into all three branches.

You could cherry-pick, but here's a good rule of thumb about when to cherry-pick: don't. The only case in which you will want to cherry-pick is when you've managed your branches badly. In this case, that might mean that you made the fix on master instead of properly branching it off from an earlier point, and people have already pulled the updates to master, so you can't change it. You'd have to cherry-pick to get it on the two release branches. But of course, you should just manage your branches right in the first place, and you'll never need to cherry-pick. (Yes, it'll still happen sometimes; such is life.)

like image 114
Cascabel Avatar answered Oct 06 '22 05:10

Cascabel