简体   繁体   中英

git apply error no such file or directory

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

1.create patch file:

 git diff --cached >> test.patch

2.use the patch:

git apply -p1 < test.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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM