Skip to content
Advertisement

Using git diff to replicate changes in another directory

I have multiple websites structured (simplified) as follows under a single GIT repository:

/
/site-1
/site-1/index.js
/site-1/about.js
/site-1/package.json
/site-1/node_modules(not versioned)
/site-1/package-lock.json(not versioned)


/site-2
/site-2/index.js
/site-2/about.js
/site-2/package.json
/site-2/node_modules(not versioned)
/site-2/package-lock.json(not versioned)

/site-3
/site-3/index.js
/site-3/about.js
/site-3/package.json
/site-3/node_modules(not versioned)
/site-3/package-lock.json(not versioned)

I did some amendments in /site-1/index.js, /site-1/package.json and added a file /site-1/changes.md. The changes were done in 2 separate git commit in a feature branch called feature/carousel.

I want to apply the same changes in /site-2 and /site-3.

I’ve tried the following:

  1. git format-patch master -o patches to retrieve the new diff in this feature branch with regards to master branch, but i was unable to apply the diff in /site-2 and /site-3.
  2. diff -ruN site-1 site-2 > PatchFile1 to generate a consolidated diff, but it takes into account files that have been modified in /site-2 as well and its not a generic diff that can be applied directly to /site-3

Any idea how to achieve this?

Advertisement

Answer

You can use git apply with the --directory option to apply a patch to another main directory, as explained here:

https://blog.soltysiak.it/en/2017/08/how-to-use-git-patch-system-to-apply-changes-into-another-folder-structure/

First, create a patch file changes.patch based on the changes applied to directory site-1. Then you can do the following:

git apply -p1 --directory='site-2' changes.patch
git apply -p1 --directory='site-3' changes.patch

-p1 removes the site folder from the patch headers, which is the main part that differs between the different directories.

--directory='site-2' will cause the site-2 prefix to be added to each header in the patch

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement