【问题标题】:How do I change paths in a unified patch?如何在统一补丁中更改路径?
【发布时间】:2015-03-28 11:40:54
【问题描述】:

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

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

补丁失败,补丁不应用错误。

有关旧的和调整后的补丁文件和 Git 输出的(部分)内容,请参见下文。我做错了什么,有没有可以使用的命令/工具来代替手动编辑补丁文件?

旧文件

    ---
 _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 path diff patch apply


    【解决方案1】:

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

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

    -p2 表示将剥离前两个组件,即a/_test_html/。

    如果没有发生错误,则在没有 --dry-run 的情况下再次运行。

    【讨论】:

    • 这使得应用(部分)补丁成为可能。我自己又犯了一个错误。我需要反转补丁而不应用它,但这并没有改变问题。由于自提交以来更改了文件,因此无法应用一些大块头(如 Joey Dorrani 所述)。为什么需要剥离 a/ 部分?我认为 a/ 和 b/ 只是针对“旧”和“新”引用。还有必要使用“补丁”与“git-apply”,因为路径也可以应用部分,而 git-apply 不能? (我自己的理解)
    • 传统上,补丁使用diff dir dir.new &gt; 1.patch 制作并应用为patch -p1 &lt; 1.patch。那些目录名实际上没有人需要,但为了使补丁与-p1“兼容”并使补丁文件更小,git 将dir/ 和dir.new/ 替换为a/ 和b/。您可以使用 --no-prefix 省略 git 中的前缀。
    【解决方案2】:
    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? 相关。也许这可以帮助你?

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多