2016-11-24 57 views
2

我有兩個文件ab,每個文件都包含它們的名稱。我創建這樣一個補丁:防止git適用於刪除舊文件

git diff a b > test.patch 

現在應用修補程序時,文件a被刪除:

git apply test.patch 

是否有辦法防止git的創建或應用修補程序時刪除文件?

+0

在應用補丁之前和之後'git status'的輸出是什麼? – rlee827

+0

@ rlee827我沒有在存儲庫中這樣做。 – timakro

回答

0

您的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> 

一種解決方法(假設你想創建添加了ab的補丁)將在臨時目錄中創建一個新的git倉庫。你可以創建一個初始使用

git commit --allow-empty -m "Initial" 

沒有文件提交和複製文件ab

運行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 
+0

這似乎是我的具體問題的正確答案,雖然未來的訪問者應該考慮使用'git diff ab'創建補丁,但是正常的補丁工具應用它:'patch -p1 -f --dry-run timakro