ashim
ashim

Reputation: 25560

How to cherry pick only changes for only one file, not the whole commit

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?

Upvotes: 143

Views: 88282

Answers (11)

binarycat
binarycat

Reputation: 190

If using the solution in @Sailesh's answer, it's important to note that, unlike git cherry-pick, git apply will either fully succeed or fail. If you want to be able to manually resolve conflicts, use git apply --3way.

Upvotes: 0

KRoy
KRoy

Reputation: 1406

Use -n option to avoid commit.

git cherry-pick -n <target>

Now do the commit by selecting desired files.

git commit -- file1 file2

Upvotes: 7

arashb31
arashb31

Reputation: 523

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

Upvotes: 5

Raghotham S
Raghotham S

Reputation: 369

Git has everything ready :)

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

Upvotes: 13

Ismail Iqbal
Ismail Iqbal

Reputation: 2580

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

Upvotes: 7

Alexander Oh
Alexander Oh

Reputation: 25621

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.

Upvotes: 2

Venkata
Venkata

Reputation: 1

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)

Upvotes: -13

0x90
0x90

Reputation: 40982

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.

Upvotes: 13

Sailesh
Sailesh

Reputation: 26197

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

Upvotes: 31

poke
poke

Reputation: 387527

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

Upvotes: 179

Ruslan Osipov
Ruslan Osipov

Reputation: 5843

This is what are you looking for:

git checkout target-branch sha1 path/to/file

sha1 is optional

Upvotes: 1

Related Questions