github - git patch file that can removedelete a file without considering its content - Stack Overflow

admin2025-05-02  0

I would like to create a .patch file that can remove/delete a file without considering its content.

In my git repo, I have myfile1.txt, its content:

Hello1
Hello2
Hello3

I want to have a git patch that will remove the file (myfile1.txt) after git apply.

And my patch (0001-delete-file.patch) look like this:

From 720a55f680189737b2af5494e189e1287c5a4207 Mon Sep 17 00:00:00 2001
From: ChunHau Tan <[email protected]>
Date: Thu, 2 Jan 2025 13:38:01 +0800
Subject: [PATCH] delete file

---
 myfile1.txt | 3 ---
 1 file changed, 3 deletions(-)
 delete mode 100644 myfile1.txt

diff --git a/myfile1.txt b/myfile1.txt
deleted file mode 100644
index 697513d..0000000
--- a/myfile1.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Hello1
-Hello2
-Hello3
-- 
2.34.1.windows.1

It looks like the patch file still contains the file's content:

@@ -1,3 +0,0 @@
-Hello1
-Hello2
-Hello3

If the file's content is changing, then I have to change the patch file.

Can we have a patch file that do the remove/delete file without considering its content ?

Please teach me how to create the patch. thank you very much !

I would like to create a .patch file that can remove/delete a file without considering its content.

In my git repo, I have myfile1.txt, its content:

Hello1
Hello2
Hello3

I want to have a git patch that will remove the file (myfile1.txt) after git apply.

And my patch (0001-delete-file.patch) look like this:

From 720a55f680189737b2af5494e189e1287c5a4207 Mon Sep 17 00:00:00 2001
From: ChunHau Tan <[email protected]>
Date: Thu, 2 Jan 2025 13:38:01 +0800
Subject: [PATCH] delete file

---
 myfile1.txt | 3 ---
 1 file changed, 3 deletions(-)
 delete mode 100644 myfile1.txt

diff --git a/myfile1.txt b/myfile1.txt
deleted file mode 100644
index 697513d..0000000
--- a/myfile1.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Hello1
-Hello2
-Hello3
-- 
2.34.1.windows.1

It looks like the patch file still contains the file's content:

@@ -1,3 +0,0 @@
-Hello1
-Hello2
-Hello3

If the file's content is changing, then I have to change the patch file.

Can we have a patch file that do the remove/delete file without considering its content ?

Please teach me how to create the patch. thank you very much !

Share Improve this question asked Jan 2 at 5:57 Anson TanAnson Tan 1,2662 gold badges14 silver badges38 bronze badges 1
  • 3 I don't think that's possible... Strictly speaking, if you are deleting a file, it is still possible to generate a tree conflicts because the file might have changed in the other branch.... The only way to make sure that is not the case is by checking its content when deleting hence the patch needs to include its content. – eftshift0 Commented Jan 2 at 6:17
Add a comment  | 

1 Answer 1

Reset to default 0
rm -f myfile.txt
git diff myfile.txt > myfile.patch
git checkout myfile.txt #Only if you want to revert to the previous state

You won't need the git diff step if your goal is to just remove the file.

rm -f myfile.txt; git add myfile.txt; git commit -m "Commit message"

Should suffice. But yes, if you want a dynamically changing patch file, first set of steps ought to help.

转载请注明原文地址:http://anycun.com/QandA/1746132316a92019.html