Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Apply part of a commit to another branch

Tags:

git

How can I apply part of a commit from one branch to another? I understand that I can cherry-pick a commit, but I need to go one step further and "cherry pick" some of the changes introduced by that commit and apply them to another (target) branch.

Is there a clean way to do this, or should I just apply the entire commit, manually undo some hunks, and remember to create more atomic commits in the future?

like image 235
Abhi Avatar asked Jul 08 '11 00:07

Abhi


People also ask

Can I merge a particular commit to another branch?

The git cherry-pick <commit> command allows you to take a single commit (from whatever branch) and, essentially, rebase it in your working branch.


1 Answers

git cherry-pick -n <SHA> will stage the changes but not commit them. You can then use git reset -p to unstage the bits you don't want, or git reset HEAD and git add -Ap to stage only the changes you want.

like image 165
dahlbyk Avatar answered Sep 21 '22 17:09

dahlbyk