135

I need to apply changes introduced in one branch to another branch. I can use cherry pick to do that. However, in my case I want to apply changes which are relevant only for one file, I don't need to cherry pick whole commit. How to do that?

Braiam
  • 1
  • 11
  • 47
  • 78
ashim
  • 24,380
  • 29
  • 72
  • 96
  • 7
    possible duplicate of [How to git-cherry-pick only changes to certain files?](http://stackoverflow.com/questions/5717026/how-to-git-cherry-pick-only-changes-to-certain-files) – Potherca Dec 14 '13 at 12:28

10 Answers10

171

You have different options based on what you want to achieve:

If you want the contents of the file to be the same as on the target branch, you can use git checkout <branch> -- <filename>. This will however not “cherry-pick” the changes that happened in a single commit, but just take the resulting state of said file. So if you added a line in a commit, but previous commits changed more, and you only want to add that line without those other changes, then a checkout is not what you want.

Otherwise if you want to apply the patch introduced in a commit to only a single file, you have multiple options. You could run git cherry-pick -n, i.e. without committing it, edit the commit (for example reset all files using git reset -- . and only add the file you actually want to change using git add <filename>). Or you could create the diff for the file and apply the diff then:

git diff <branch>^..<branch> -- <filename> | git apply
poke
  • 369,085
  • 72
  • 557
  • 602
28

Create a patch file and apply it.

git diff branchname -- filename > patchfile
git apply patchfile

EDIT:

Since you need to take the changes from a commit, create the patch like this:

git show sha1 -- filename > patchfile
Sailesh
  • 25,517
  • 4
  • 34
  • 47
  • 1
    -1 `git checkout` with a filename is the right hammer for this nail. Both of these options are needlessly complex. – Rein Henrichs Apr 17 '13 at 21:14
  • 9
    For this particular case, yes. But in general, if you want to pick changes from a commit made to a particular file, then `checkout` will not work, since it may involve other unwanted previous changes, and may not contain changes made in the current branch. – Sailesh Apr 18 '13 at 00:17
  • As @Alexander Oh points it out (https://stackoverflow.com/a/42623347/414075), the `sha1` is the blob id of the file in the commit. `git ls-tree -r` helps in this regard. But the result will be the actual file content rather then the patch. – white_gecko Nov 08 '22 at 17:01
  • Without temporary file: `git diff branchname -- filename | git apply -` – Eric M. Aug 25 '23 at 16:28
13

Another handy thing to do is get the patch locally and then use:

git checkout {<name_of_branch>, commit's SHA} <path to the file> 

That's not a cherry-picking though.

0x90
  • 39,472
  • 36
  • 165
  • 245
  • 2
    Warning: as @poke says in his answer, this does not copy the changes over; it copies the entire state of the file. So if you want to _add_ a commit's changes to the file, then by checking out like this you end up _overwriting_ any newer changes which is not good. – Alexander Bird Sep 08 '14 at 15:46
12

Git has everything ready :)

Just use git checkout <sha> <path-to-file>

Raghotham S
  • 351
  • 3
  • 4
  • 12
    That's not a cherry-pick -- that checks out the file in full. The goal is to make a particular change from a commit. Often those are the same thing, but sometimes they are not. – AndrewF Sep 15 '17 at 19:47
7
git checkout the_branch_with_the_change the/path/to/the/file/you/want.extension

this works if you want a single file from another branch to be copied to the current working branch

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46
6

Use -n option to avoid commit.

git cherry-pick -n <target>

Now do the commit by selecting desired files.

git commit -- file1 file2
KRoy
  • 1,290
  • 14
  • 10
5

You can try doing this:

git show COMMIT_ID -- path/to/specific-file | git apply

For example:

git show 3feaf20 -- app/views/home/index.html | git apply
arashb31
  • 473
  • 6
  • 12
2

What I tend to do is to use git ls-tree

just do:

git ls-tree -r <commit-id> |grep 'your filename'
# there will be output that shows a SHA1 of your file
git show ${SHA1 of your file} > 'your filename'

This will print all filenames matching your grep and gives SHA1 keys of the files in the commit.

Git show can be used on files outside of your branch as well. using > from your shell to pipe the output into a file gives you the result you are looking for.

Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
  • 1
    `git ls-tree -r` was the missing part over @Sailesh s answer (https://stackoverflow.com/a/16068419/414075) thank you – white_gecko Nov 08 '22 at 16:59
1

This is what are you looking for:

git checkout target-branch sha1 path/to/file

sha1 is optional

Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44
-12
git reset HEAD~1

moves the files into pre-commit stage

git stash

removes from memory

Now your branch is pretty clean (reverted to previous commit)

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
Venkata
  • 1
  • 4