makansij
makansij

Reputation: 9865

Is a new branch necessary before git cherry pick?

I've been following this example here, and it shows that you create a temporary branch before you use git cherry-pick, and then you cherry pick onto that temporary branch.

In the example, the temporary branch is called newbar, and it branches off from foo.

Is it necessary to have the temporary branch? Or can you just cherry pick from foo? The example doesn't make it clear the purpose of this temporary branch.

Upvotes: 3

Views: 390

Answers (2)

U r s u s
U r s u s

Reputation: 6978

It is not necessary to create a new branch for cherry-picking. The reason the example does it is because is simulating the rebase command.

This example is clearer

If you were at node H in this graph, and you typed git cherry-pick E (yes, you'd actually type part or all of the SHA for the commit, but for simplicity's sake, I'll just use the labels that are already here), you'd wind up with a copy of commit E—let's call it "E prime" or E'—that pointed to H as its parent, like so:

enter image description here

The important thing to notice here is that Git has copied changes made in one place, and replayed them somewhere else.

Upvotes: 1

Thomas Stringer
Thomas Stringer

Reputation: 5862

No, you don't need to have a separate branch. This is perfectly legal:

$ git log --decorate --oneline --graph --all
* f1188b1 (HEAD -> master) commit3
* 90a233e commit2
* f167481 commit1

Then while still in the single and only branch master:

$ git cherry-pick 90a233e

The catch here is that it's quite possible to have a conflict (below is sample output after a cherry-pick):

error: could not apply 90a233e... commit2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

Simply handle and resolve, as per git-status:

$ git status
On branch master
You are currently cherry-picking commit 90a233e.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")

Basically you'll fix your conflict, and then add the changes to the index and run $ git cherry-pick --continue to complete the cherry pick operation.

Upvotes: 1

Related Questions