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.
This question is related to
git
git-cherry-pick
If "partly cherry picking" means "within files, choosing some changes but discarding others", it can be done by bringing in git stash
:
git reset HEAD^
to convert the entire cherry-picked commit into unstaged working changes.git stash save --patch
: interactively select unwanted material to stash.git commit
git stash drop
. Tip: if you give the stash of unwanted changes a name: git stash save --patch junk
then if you forget to do (6) now, later you will recognize the stash for what it is.
Building on Mike Monkiewicz answer you can also specify a single or more files to checkout from the supplied sha1/branch.
git checkout -p bc66559 -- path/to/file.java
This will allow you to interactively pick the changes you want to have applied to your current version of the file.
If you want to specify a list of files on the command line, and get the whole thing done in a single atomic command, try:
git apply --3way <(git show -- list-of-files)
--3way
: If a patch does not apply cleanly, Git will create a merge conflict so you can run git mergetool
. Omitting --3way
will make Git give up on patches which don't apply cleanly.
Use git format-patch
to slice out the part of the commit you care about and git am
to apply it to another branch
git format-patch <sha> -- path/to/file
git checkout other-branch
git am *.patch
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?
Actually, the best solution for this question is to use checkout
commend
git checkout <branch> <path1>,<path2> ..
For example, assume you are in master, you want to the changes from dev1 on project1/Controller/WebController1.java
and project1/Service/WebService1.java
, you can use this:
git checkout dev1 project1/Controller/WebController1.java project1/Service/WebService1.java
That means the master branch only updates from dev1 on those two paths.
Assuming the changes you want are at the head of the branch you want the changes from, use git checkout
for a single file :
git checkout branch_that_has_the_changes_you_want path/to/file.rb
for multiple files just daisy chain :
git checkout branch_that_has_the_changes_you_want path/to/file.rb path/to/other_file.rb
Source: Stackoverflow.com