14

I have two sepearate git repos: A and B. Some repository B files are already present in a subfolder of project A. My goal is to create patches for repo B and then applying them to the subfolder within repo A to conserve history of repo B while merging them. The issue is that a patch is unable to create new files. For example:

assuming this folder structure: /home/user/B/..bunch of directories and /home/user/A/ext/lib/B/..bunch of directories

cd /home/user/B

git format-patch "xx..xx" -o /home/user/A/ (create patch files)

cd /home/user/A

git apply -v --directory=ext/lib/B/ 0001-foo-12345.patch

works fine since the patch is not creating any new files or trying to access a folder which is present in B but not A

BUT

cd /home/user/A

git apply -v --directory=ext/lib/B/ 0002-foo2-6789.patch

does not work and throws this error: Checking patch ext/lib/B/xyz/test.c... error: ext/lib/B/xyz/test.c: No such file or directory.

I have tried the following commands so far:

git apply -v --directory=/home/user/A/lib/B/ --include=bb/cc --exclude=cc/ --exclude=bb/ --include=* 0002-foo2-6789.patch

git apply -v --directory=/home/user/A/lib/B/ --include=* --include=bb/cc --exclude=cc/ --exclude=bb/ 0002-foo2-6789.patch

git am --directory=/home/user/A/lib/B/ --include=* --include=bb/cc --exclude=cc/ --exclude=bb/ 0002-foo2-6789.patch

sid99
  • 141
  • 1
  • 1
  • 3
  • When you're generating patches from the files in B, does one of the commits in the range create the file `ext/lib/B/xyz/test.c`, or is it already existing before those commits? – bk2204 Jun 07 '19 at 21:32
  • `ext/lib/B/xyz/test.c` is not present before the commits and ideally (as far as I know) it should be added when I try to do **git apply** – sid99 Jun 07 '19 at 23:22

2 Answers2

3

1.create patch file:

 git diff --cached >> test.patch

2.use the patch:

git apply -p1 < test.patch
dong sheng
  • 266
  • 3
  • 10
1

There are a number of ways to create patch files that will create new files. However, creating patches in repo B and applying them in repo A won't import the history of repo B into repo A. Is that what you mean by "conserve history of repo B while merging them"?

Example patch that causes git apply to create a new path:

diff --git a/b1.txt b/b1.txt
new file mode 100644            <-- lines specific to creating new files
index 0000000..12f00e9
--- /dev/null                   <-- lines specific to creating new files
+++ b/b1.txt
@@ -0,0 +1 @@
+contents

One way to create patches like this is to copy the files from repo B to their destination in repo A, git add any changed or new files, then use git diff --staged > my.patch to create a patch for all changed and new files. However, by then, the files are already in repo A, so there's little point in creating a such patch, and this also won't import repo B's history into repo A.

If you really want to merge repo B into a subdirectory of repo A and preserver the history of both, you're better off not using patches and looking at the top several answers here: How do you merge two Git repositories?

webb
  • 4,180
  • 1
  • 17
  • 26