Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cherry-pick changes from one file to another file?

I have two files called vvn.c and aqu.c I did changes to vvn.c and I have that commit in my git. How to cherry-pick the same changes to aqu.c The only difference is the API's.

vvn.c contains API's as vvn_function_names()

whereas aqu.c contains API's as unions_function_names()

I don't know how to do that. I know about cherry-picking into the same file. Is there any way to do that?

like image 344
optimus prime Avatar asked May 27 '16 12:05

optimus prime


People also ask

Can we cherry pick commit from one repo to another?

It is possible to cherry pick from another repo using the command line. You will first need to add the other repository as a remote and then fetch the changes. From there, you should be able to see the commit in your repo and cherry pick it.


2 Answers

Not exactly cherry-pick, but you might want to use:

git checkout <branch or sha of commit> -- filename

This checkouts the file by its version in another branch; it is the easiest possible solution, if it satisfy your needs (i.e., if you don't need to actually merge changes to the file from two separate branches)

The other possibility is to generate diff (you can generate diff from a range of commits just and limit it to one file) and then apply the diff. Check out: How to diff the same file between two different commits on the same branch?

like image 198
Tomas Kulich Avatar answered Nov 01 '22 22:11

Tomas Kulich


Basically you need to make a diff on the vvn.c changes and apply them to a aqu.c. See e.g. here: How to apply a Git patch to a file with a different name and path?

However, this is quite tricky thing. Why do you have different file names which anyhow similar so that you should make same changes?

Probably it is better to come up with some another approach. E.g. for a C you could use #include and extract common part into a separate file.

like image 23
kan Avatar answered Nov 01 '22 22:11

kan