如何更改统一补丁中的路径?

How do I change paths in a unified patch?

我有一个 Git 存储库,我在其中对文件夹结构进行了一些更改。我将 ./_test_html 目录中的所有内容移动到存储库的根目录 ./ 并继续工作。

现在我想还原在目录更改之前所做的一些更改,但意识到我无法通过正常还原来执行此操作,因为受影响的文件不再存在于我当前的分支中,所以我创建了一个补丁文件并将文件中的所有路径从 ./subdir 更改为 ./ 以使引用再次正确,但是 git apply 给我错误:

patch failed, patch does not apply errors.

请参阅下面的(部分)旧补丁文件和调整后的补丁文件以及 Git 输出。我做错了什么,是否可以使用 command/tool 而不是手动编辑补丁文件?

旧文件

    ---
 _test_html/mobile/css/sf.css                 |  53 ++++++++++++++--
 _test_html/mobile/images/logo_temp_x.png     | Bin 17636 -> 19362 bytes
 _test_html/mobile/js/secretflirt.js          |  16 ++++-
 _test_html/mobile/templates/footer.tpl.php   |   7 ++-
 _test_html/mobile/templates/header.tpl.php   |  90 ++++++++++++++++++++++++++-
 _test_html/mobile/templates/ingelogd.tpl.php |  40 +-----------
 6 files changed, 157 insertions(+), 49 deletions(-)

diff --git a/_test_html/mobile/css/sf.css b/_test_html/mobile/css/sf.css
index 0d5f317..63a3b9d 100644
--- a/_test_html/mobile/css/sf.css
+++ b/_test_html/mobile/css/sf.css
@@ -66,7 +66,7 @@ h2, .ui-collapsible-heading{margin:0;font-size:16px;}

 .ui-collapsible-heading a span.ui-btn{right:6px;}

-.ui-field-contain{ border-bottom-width: 0px!important; 0px; padding: 0px;
+.ui-field-contain{ border-bottom-width: 0px!important; padding: 0px;
 margin: 3px 3px 0px 0px; width: 100%;}
 input.ui-input-text, .ui-input-search{background:#fff;color:#333;margin: 2px 2px 2px 0px; width: 100%;height: 2.1em;}
 textarea.ui-input-text {background:#fff;color:#333;margin-bottom: 5px; margin-left: 0px;margin-right: 5px;width: 100%; min-height:150px;}
@@ -648,7 +648,7 @@ label.ui-input-text, label.ui-select{color:#333;font-size:100%;font-weight:norma
 .ui-body-c .ui-footer .ui-link{color:#c8c8c8;font-size:110%;font-weight:bold;}
 .ui-body-c .ui-footer .ui-link.last{color:#2489CE;}
 .ui-body-c .ui-footer .ui-link:hover{color:#fff;}
-label.error, {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
+label.error {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
 color: red;
 font-size: 1.1em;}

新建文件

    ---
 mobile/css/sf.css                 |  53 ++++++++++++++--
 mobile/images/logo_temp_x.png     | Bin 17636 -> 19362 bytes
 mobile/js/secretflirt.js          |  16 ++++-
 mobile/templates/footer.tpl.php   |   7 ++-
 mobile/templates/header.tpl.php   |  90 ++++++++++++++++++++++++++-
 mobile/templates/ingelogd.tpl.php |  40 +-----------
 6 files changed, 157 insertions(+), 49 deletions(-)

diff --git a/mobile/css/sf.css b/mobile/css/sf.css
index 0d5f317..63a3b9d 100644
--- a/mobile/css/sf.css
+++ b/mobile/css/sf.css
@@ -66,7 +66,7 @@ h2, .ui-collapsible-heading{margin:0;font-size:16px;}

 .ui-collapsible-heading a span.ui-btn{right:6px;}

-.ui-field-contain{ border-bottom-width: 0px!important; 0px; padding: 0px;
+.ui-field-contain{ border-bottom-width: 0px!important; padding: 0px;
 margin: 3px 3px 0px 0px; width: 100%;}
 input.ui-input-text, .ui-input-search{background:#fff;color:#333;margin: 2px 2px 2px 0px; width: 100%;height: 2.1em;}
 textarea.ui-input-text {background:#fff;color:#333;margin-bottom: 5px; margin-left: 0px;margin-right: 5px;width: 100%; min-height:150px;}
@@ -648,7 +648,7 @@ label.ui-input-text, label.ui-select{color:#333;font-size:100%;font-weight:norma
 .ui-body-c .ui-footer .ui-link{color:#c8c8c8;font-size:110%;font-weight:bold;}
 .ui-body-c .ui-footer .ui-link.last{color:#2489CE;}
 .ui-body-c .ui-footer .ui-link:hover{color:#fff;}
-label.error, {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
+label.error {display:block;margin-left: 10px;margin-top: 5px;text-shadow: 1px 1px 5px white;
 color: red;
 font-size: 1.1em;}

git 应用输出

    $ git apply patch2.diff
patch2.diff:101: trailing whitespace.
    min-height:400px
patch2.diff:843: trailing whitespace.
            }
patch2.diff:846: trailing whitespace.
});
patch2.diff:873: trailing whitespace.
<div class="panel-content">
patch2.diff:874: trailing whitespace.

error: patch failed: mobile/css/sf.css:66
error: mobile/css/sf.css: patch does not apply
error: the patch applies to 'mobile/images/logo_temp_x.png' (ebe43e4cce1686add4f
ad9bf64f0fc261010a6b5), which does not match the current contents.
error: mobile/images/logo_temp_x.png: patch does not apply
error: patch failed: mobile/js/secretflirt.js:1518
error: mobile/js/secretflirt.js: patch does not apply
error: patch failed: mobile/templates/footer.tpl.php:1
error: mobile/templates/footer.tpl.php: patch does not apply
error: patch failed: mobile/templates/header.tpl.php:1
error: mobile/templates/header.tpl.php: patch does not apply
error: patch failed: mobile/templates/ingelogd.tpl.php:28
error: mobile/templates/ingelogd.tpl.php: patch does not apply

Warning: Your console font probably doesn't support Unicode. If you experience s
trange characters in the output, consider switching to a TrueType font such as L
ucida Console!
git apply example.patch
error: patch failed: includes/example.inc:233
error: includes/example.inc: patch does not apply

是一个经常重复出现的错误。

我了解到发生这种情况是因为 git 无法应用补丁中的更改,因为它无法找到有问题的代码行;它们必须已被另一次提交更改或删除。

看起来与 How to apply patches on the top of a git tree preventing duplication? 有关。也许这可以帮助你?

您可以尝试在干燥模式下使用 patch 应用原始补丁:

patch -p2 --dry-run -i patch2.diff 

-p2表示前两个分量将被剥离,即a/_test_html/.

如果没有发生错误,那么 运行 没有 --dry-run