Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partly cherry-picking a commit with Git

I'm working on 2 different branches: release and development.

I noticed I still need to integrate some changes that were committed to the release branch back into the development branch.

The problem is I don't need all of the commit, only some hunks in certain files, so a simple

git cherry-pick bc66559 

does not do the trick.

When I do a

git show bc66559 

I can see the diff but don't really know a good way of applying that partially to my current working tree.

like image 719
oliver Avatar asked Oct 06 '09 14:10

oliver


People also ask

Can you cherry pick a commit?

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

How do you commit cherry pick changes?

To change the commit message when cherry-picking, use “git cherry-pick” with the “-e” option. As illustrated in this example, your default editor will open and it will let you change the commit message. When you are satisfied with the edits, save your file and your commit message should be saved successfully.

How do you cherry pick a merge commit?

With the cherry-pick command, Git lets you incorporate selected individual commits from any branch into your current Git HEAD branch. When performing a git merge or git rebase , all the commits from a branch are combined. The cherry-pick command allows you to select individual commits for integration.

Does cherry pick change commit hash?

When you are performing a regular cherry-pick, you will get a new commit hash but the commit message will be the same. However, there is a way to append the origin of a cherry-pick to the commit message : by using the cherry-pick with the “-x” option.


2 Answers

The core thing you're going to want here is git add -p (-p is a synonym for --patch). This provides an interactive way to add in content, letting you decide whether each hunk should go in or not, and even letting you manually edit the patch if necessary.

To use it in combination with cherry-pick:

git cherry-pick -n <commit> # get your patch, but don't commit (-n = --no-commit) git reset                   # unstage the changes from the cherry-picked commit git add -p                  # make all your choices (add the changes you do want) git commit                  # make the commit! 

(Thanks to Tim Henigan for reminding me that git-cherry-pick has a --no-commit option, and thanks to Felix Rabe for pointing out that you need to git reset. If you only want to leave a few things out of the commit, you could use git reset <path>... to unstage just those files.)

You can provide specific paths to add -p if necessary. If you're starting with a patch you could replace the cherry-pick with apply.


If you really want to git cherry-pick -p <commit> (that option does not exist), you can use

git checkout -p <commit> 

That will diff the current commit against the commit you specify, and allow you to apply hunks from that diff individually. This option may be more useful if the commit you're pulling in has merge conflicts in part of the commit you're not interested in. (Note, however, that checkout differs from cherry-pick: checkout tries to apply <commit>'s contents entirely, while cherry-pick applies the diff of the specified commit from it's parent. This means that checkout can apply more than just that commit, which might be more than you want.)

like image 65
Cascabel Avatar answered Oct 24 '22 10:10

Cascabel


I know I'm answering an old question, but it looks like there's a new way to do this with interactively checking out:

git checkout -p bc66559 

Credit to Can I interactively pick hunks from another git commit?

like image 35
Mike Monkiewicz Avatar answered Oct 24 '22 10:10

Mike Monkiewicz