我有两个文件a
和b
,每个文件都包含它们的名称。我创建这样一个补丁:防止git适用于删除旧文件
git diff a b > test.patch
现在应用修补程序时,文件a
被删除:
git apply test.patch
是否有办法防止git的创建或应用修补程序时删除文件?
我有两个文件a
和b
,每个文件都包含它们的名称。我创建这样一个补丁:防止git适用于删除旧文件
git diff a b > test.patch
现在应用修补程序时,文件a
被删除:
git apply test.patch
是否有办法防止git的创建或应用修补程序时删除文件?
您的git diff
使用的语法是
git diff [--no-index] <path> <path>
在你的情况下,--no-index
假设隐含。因此,命令
git diff a b
假定b
是文件a
的较新版本。运行你的命令产生补丁,看起来像这样:
$ git diff --no-index 123.txt 456.txt
diff --git a/123.txt b/456.txt
index 97f93f4..9fa0bac 100644
--- a/123.txt
+++ b/456.txt
@@ -1 +1 @@
-123.txt
+456.txt
如果你尝试用超过2个文件要做到这一点,有一个错误:
$ git diff -- 123.txt 456.txt 789.txt
Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>
$ git diff --no-index 123.txt 456.txt 789.txt
usage: git diff --no-index <path> <path>
一种解决方法(假设你想创建添加了a
和b
的补丁)将在临时目录中创建一个新的git仓库。你可以创建一个初始使用
git commit --allow-empty -m "Initial"
没有文件提交和复制文件a
和b
在
运行git add --all
后,git diff --staged > test.patch
将创建一个适当的补丁,这样的:
diff --git a/123.txt b/123.txt
new file mode 100644
index 0000000..97f93f4
--- /dev/null
+++ b/123.txt
@@ -0,0 +1 @@
+123.txt
diff --git a/456.txt b/456.txt
new file mode 100644
index 0000000..9fa0bac
--- /dev/null
+++ b/456.txt
@@ -0,0 +1 @@
+456.txt
diff --git a/789.txt b/789.txt
new file mode 100644
index 0000000..a25ef22
--- /dev/null
+++ b/789.txt
@@ -0,0 +1 @@
+789.txt
这似乎是我的具体问题的正确答案,虽然未来的访问者应该考虑使用'git diff ab'创建补丁,但是正常的补丁工具应用它:'patch -p1 -f --dry-run
在应用补丁之前和之后'git status'的输出是什么? – rlee827
@ rlee827我没有在存储库中这样做。 – timakro