git cherry picking one commit to another branch

2019-09-20 00:04发布

问题:

In developing for a work enviornment, we create a branch for each feature or fix we work on, and then merge to QA for testing.

Occasionally I find it's helpful to have a stash which contains console.logs' of debugging steps for a specific process. But I want to only have that be a subset of files in my working copy.

So, I am considering creating another branch for local use with commits which will be used later. Which don't reference a specific ticket necessarily.

So the question is, how can I take a specific commit in this "useful items" branch, and apply it to another branch? I'm sorry if I'm using the term cherry pick incorrectly here. I use sourcetree but am able to use command line for this also.

回答1:

there are several ways to achieve it (you have to be on the branch you want to apply the commit to),

you can create a patch of the commit you want then apply it

git format-patch <commit sha1> 
git apply <patch path>

or you can indeed cherry-pick the commit you want

git cherry-pick <commit sha1>