How to git-cherry-pick only changes to certain files?
I usually use the -p
flag with a git checkout from the other branch which I find easier and more granular than most other methods I have come across.
In principle:
git checkout <other_branch_name> <files/to/grab in/list/separated/by/spaces> -p
example:
git checkout mybranch config/important.yml app/models/important.rb -p
You then get a dialog asking you which changes you want in "blobs" this pretty much works out to every chunk of continuous code change which you can then signal y
(Yes) n
(No) etc for each chunk of code.
The -p
or patch
option works for a variety of commands in git including git stash save -p
which allows you to choose what you want to stash from your current work
I sometimes use this technique when I have done a lot of work and would like to separate it out and commit in more topic based commits using git add -p
and choosing what I want for each commit :)
Perhaps the advantage of this method over Jefromi's answer is that you don't have to remember which behaviour of git reset is the right one :)
# Create a branch to throw away, on which we'll do the cherry-pick:
git checkout -b to-discard
# Do the cherry-pick:
git cherry-pick stuff
# Switch back to the branch you were previously on:
git checkout -
# Update the working tree and the index with the versions of A and B
# from the to-discard branch:
git checkout to-discard -- A B
# Commit those changes:
git commit -m "Cherry-picked changes to A and B from [stuff]"
# Delete the temporary branch:
git branch -D to-discard
I'd do it with cherry-pick -n
(--no-commit
) which lets you inspect (and modify) the result before committing:
git cherry-pick -n <commit>
# unstage modifications you don't want to keep, and remove the
# modifications from the work tree as well.
# this does work recursively!
git checkout HEAD <path>
# commit; the message will have been stored for you by cherry-pick
git commit
If the vast majority of modifications are things you don't want, instead of checking out individual paths (the middle step), you could reset everything back, then add in what you want:
# unstage everything
git reset HEAD
# stage the modifications you do want
git add <path>
# make the work tree match the index
# (do this from the top level of the repo)
git checkout .
The other methods didn't work for me since the commit had a lot of changes and conflicts to a lot of other files. What I came up with was simply
git show SHA -- file1.txt file2.txt | git apply -
It doesn't actually add
the files or do a commit for you so you may need to follow it up with
git add file1.txt file2.txt
git commit -c SHA
Or if you want to skip the add you can use the --cached
argument to git apply
git show SHA -- file1.txt file2.txt | git apply --cached -
You can also do the same thing for entire directories
git show SHA -- dir1 dir2 | git apply -