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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With