git apply error no such file or directory
Asked Answered
O

3

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

Octavia answered 7/6, 2019 at 21:2 Comment(2)
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?Kyrakyriako
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 applyOctavia
H
3

1.create patch file:

 git diff --cached >> test.patch

2.use the patch:

git apply -p1 < test.patch
Hubbard answered 23/5, 2022 at 2:27 Comment(0)
L
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?

Lorollas answered 12/6, 2021 at 13:13 Comment(0)
D
-2
  1. Use git apply for your patch.
  2. Exclude specific files with --exclude, like this:
git apply patch --exclude=a.rb --exclude=b.rb

Alternatively, if automation isn't crucial, manually edit the patch file using a text editor. Remember, a patch is just plain text, so you can remove what you don't need.

Demicanton answered 6/10, 2023 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.