Ryan Leahy
Ryan Leahy

Reputation: 121

copy git diff from one file to another

I have 2 files which are almost entirely the same only one has a few specific lines for development environments that the other does not have. I want to be able to copy changes made to the "production" file , and include those changes in the appropriate place in the "development" file. For example, when a new script tag is included on line 55 on the production file, I could use (a git command most likely) something to copy that change, find the line it needs to go on in the development file and paste it there, so the files are kept in sync.

Upvotes: 0

Views: 814

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601609

You can use the patch command-line utility to achieve this. Assuming you edited file-a, but haven't staged your changes, and you now want to apply the same patch to file-b, you can do

git diff file-a | patch file-b

Diff hunks that didn't apply cleanly will be written to a new file, and there will be diagnostic messages on the screen.

You might consider whether you really want to keep that redundancy. Can't you refactor the files in a way that they are based on a common module or include file, so you never have to make the same change to both of the files? Or alternativley, if whatever language the files are written in doesn't support this kind of abstraction, add some preprocessing that builds one file from the other?

Keeping the files in sync manually seems cumbersome and error-prone.

Upvotes: 2

Related Questions