【问题标题】:If I create a patch from local, uncommited work - it fails. If I commit my work, then create patch, it applies without fail. Why?如果我从本地未提交的工作创建补丁 - 它会失败。如果我提交我的工作,然后创建补丁,它会应用而不会失败。为什么?
【发布时间】:2019-12-02 21:05:37
【问题描述】:

我想根据我的本地代码和远程源代码之间的差异创建一个补丁。

我这样做:

git diff origin/myTestBranch > myPatch.patch 

我进入一个单独的目录,该目录克隆了相同的 repo 并刚刚拉取了 myTestBranch。我这样做是用

cd testPatchRepo
git clone ....
git checkout myTestBranch

当我尝试应用补丁时 - 它失败了!

但是,如果我提交我的工作并使用 git diff firstCommit ^...lastCommit > myPatch.patch 创建补丁 补丁应用没有问题...这并不能解决我的问题,因为我仍然希望能够从我的本地、未提交的工作中创建补丁。

这是从本地未提交的工作中创建的补丁:

 diff --git a/scripts b/scripts
index 8640468..adfabd7 160000
--- a/scripts
+++ b/scripts
@@ -1 +1 @@
-Subproject commit 8640468cc4ba87bebee0e9e26939361bcb3e3afa
+Subproject commit adfabd7bf11566f2888a4501bc557265f1172ac2-dirty
diff --git a/source/tutorial/install.txt b/source/tutorial/install.txt
index 5fee5f6..5ef6e4b 100644
--- a/source/tutorial/install.txt
+++ b/source/tutorial/install.txt
@@ -16,7 +16,7 @@ Install {+bi-short+} on Windows

 .. include:: /includes/fact-bi-enterprise.rst

-Massaddie
+Maaaaassaddie
 To set up |bi|,
 follow the steps on this page.

提交我的更改后创建的补丁:

commit 983393a03262f8dccfd90b3c948751846ae583ae
Author: maddie <myemail>
Date:   Mon Dec 2 17:17:42 2019 -0500

    commit before make stage commit

diff --git a/source/tutorial/install.txt b/source/tutorial/install.txt
index 5fee5f6..5ef6e4b 100644
--- a/source/tutorial/install.txt
+++ b/source/tutorial/install.txt
@@ -16,7 +16,7 @@ Install {+bi-short+} on Windows

 .. include:: /includes/fact-bi-enterprise.rst

-Massaddie
+Maaaaassaddie
 To set up |bi| with a business intelligence tool such as Tableau,
 follow the steps on this page.

【问题讨论】:

  • 在创建补丁之前运行git pull --rebase origin ......需要替换为远程分支名称)。另外,您真的有一个名为 myTestBranch 的远程分支吗?你是如何克隆并切换到它的?
  • 它实际上被称为 test2,但为了这篇文章的目的,我给它起了一个更好的名字。我 fork 一个 repo,克隆它并使用 git checkout test2 切换到分支
  • 我希望能够对我在本地拥有的任何东西进行 git diff - 甚至是未提交的工作。 git pull --rebase origin/.... 要求提交或隐藏我的所有更改
  • 我的意思是你的基础和克隆的 repo 上的不一样。
  • 在做git pull --rebase之前有没有办法验证它们的碱基不同?

标签: git diff patch


【解决方案1】:

虽然通常您可以将git diff 的输出应用到另一个工作树,但在这种情况下您不能。原因是git diff 表明您的子模块是脏的:也就是说,它有未提交的更改。由于无法在 diff 中表示这些更改,因此任何应用它们的尝试都将失败。

如果您想排除这些子模块更改,您可以使用git diff --ignore-submodules,它将忽略您的子模块,无论是否修改。如果你想递归地包含它们,你可以使用git diff --submodule=diff。该补丁不会更新子模块提交本身,但它会更改子模块工作树,因此不推荐。

这两者都应该产生适用的补丁。

【讨论】:

  • 奇怪的是,我没有从这些命令中获得适用的补丁。当我执行git apply -v mypatch.patch 时,我仍然收到“搜索时出错”
  • 可能是因为补丁包含对本地未推送提交的更改——远程版本没有(因此无法找到)
  • 我也有点困惑——我没有使用子模块
  • @maddie 你做的——主仓库根目录scripts 是一个子模块,目前是脏的(有一个新的非推送提交);您应该将其推拉到您尝试应用补丁的位置。
猜你喜欢
  • 1970-01-01
  • 2014-11-28
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 2011-07-06
  • 1970-01-01
  • 2011-06-09
相关资源
最近更新 更多