git-add和git-diff使用不同的差异工具?

发布于 2025-01-29 17:30:42 字数 2115 浏览 2 评论 0原文

tl; dr I配置了一个差异和git-diff给出了“智能”差异,但git-add创建了“愚蠢的” hunks。为什么?

我配置了difftool以使用 nbdime 带有nbdime config -git -able -able -git- -global,我认为本质上只是将这些行添加到我的.gitConfig:

[diff "jupyternotebook"]
    command = git-nbdiffdriver diff
[merge "jupyternotebook"]
    driver = git-nbmergedriver merge %O %A %B %L %P
    name = jupyter notebook merge driver
[difftool "nbdime"]
    cmd = git-nbdifftool diff \"$LOCAL\" \"$REMOTE\" \"$BASE\"
[difftool]
    prompt = false
[mergetool "nbdime"]
    cmd = git-nbmergetool merge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
[mergetool]
    prompt = false

现在git diff给出了我期望的好输出:

nbdiff /var/folders/6b/03yw1pts2nx_q8vftrh6fv140000gp/T//FILE.ipynb FOLDER/FILE.ipynb
--- /var/folders/6b/03yw1pts2nx_q8vftrh6fv140000gp/T//FILE.ipynb  2022-05-17 14:29:39.937318
+++ FOLDER/FILE.ipynb  2022-05-17 14:09:45.222229
## inserted before /cells/0:
+  code cell:
+    source:
+      ...
+  markdown cell:
+    source:
+      ...

## deleted /cells/0:
-  markdown cell:
-    source:
-      ...

## inserted before /cells/2:
+  code cell:
+    source:
+      ...

但是,如果我执行git git git add add -e folder/files/file .ipynb,它给我一个“非常糟糕的”差异:

diff --git a/FOLDER/FILE.ipynb b/FOLDER/FILE.ipynb
index 3a1540c..17363f8 100644
--- a/FOLDER/FILE.ipynb
+++ b/FOLDER/FILE.ipynb
@@ -1,621 +1,716 @@
 {
- "cells": [
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    ...
-   ]
-  },
-  ... almost every line in the file is removed
+  "cells": [
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "id": "j1qKT6qtAYEj"
+      },
+      "outputs": [],
+      "source": [
+        ...
+      ]
+    },
+    ... almost every line in the file is added back

我可能对git-add做什么有根本的误解,但是为什么不使用nbdime diff工具git add?有什么方法可以添加我在git-diff中看到的更改?

TL;DR I configured a difftool and git-diff gives "intelligent" diffs but git-add creates "stupid" hunks. Why?

I configured the difftool to use nbdime with nbdime config-git --enable --global which I think essentially just adds these lines to my .gitconfig:

[diff "jupyternotebook"]
    command = git-nbdiffdriver diff
[merge "jupyternotebook"]
    driver = git-nbmergedriver merge %O %A %B %L %P
    name = jupyter notebook merge driver
[difftool "nbdime"]
    cmd = git-nbdifftool diff \"$LOCAL\" \"$REMOTE\" \"$BASE\"
[difftool]
    prompt = false
[mergetool "nbdime"]
    cmd = git-nbmergetool merge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
[mergetool]
    prompt = false

Now git diff gives the good output I expect:

nbdiff /var/folders/6b/03yw1pts2nx_q8vftrh6fv140000gp/T//FILE.ipynb FOLDER/FILE.ipynb
--- /var/folders/6b/03yw1pts2nx_q8vftrh6fv140000gp/T//FILE.ipynb  2022-05-17 14:29:39.937318
+++ FOLDER/FILE.ipynb  2022-05-17 14:09:45.222229
## inserted before /cells/0:
+  code cell:
+    source:
+      ...
+  markdown cell:
+    source:
+      ...

## deleted /cells/0:
-  markdown cell:
-    source:
-      ...

## inserted before /cells/2:
+  code cell:
+    source:
+      ...

But if I do git add -e FOLDER/FILE.ipynb, it gives me a "really bad" diff:

diff --git a/FOLDER/FILE.ipynb b/FOLDER/FILE.ipynb
index 3a1540c..17363f8 100644
--- a/FOLDER/FILE.ipynb
+++ b/FOLDER/FILE.ipynb
@@ -1,621 +1,716 @@
 {
- "cells": [
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    ...
-   ]
-  },
-  ... almost every line in the file is removed
+  "cells": [
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "id": "j1qKT6qtAYEj"
+      },
+      "outputs": [],
+      "source": [
+        ...
+      ]
+    },
+    ... almost every line in the file is added back

I may have a fundamental misunderstanding of what git-add does, but why isn't git add using the nbdime diff tool? And is there a way I can add just the changes that I see in git-diff ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2025-02-05 17:30:42

git add -egit add -p都需要能够理解编辑 diff。他们通常对DIFF的理解有限,并且需要Plain git diff的“愚蠢”格式。 nbdime工具将原始文件分开,将其重新拆除为可用的文本,然后差异可用的文本, 1 ,但这不是的实际 >文件和git add -e需要在文件中的上进行工作,而不是其清理介绍。


1 文件中的内容是机器可读的JSON。 nbdime工具的结果似乎是yaml。如果Git具有本机JSON DIFF引擎,git add -p,并且公司将能够处理结果,但是Git却没有,因此不是。如果Jupyter Notebook使用YAML,则Git的面向线路的工具将能够处理它们,但是Jupyter-Notebook却没有,因此不是。

Both git add -e and git add -p need to be able to understand an edited diff. They have a limited amount of comprehension of diffs in general, and require the "dumb" format from plain git diff. The nbdime tools take the original files apart, re-shuffle them into usable text, and diff that usable text,1 but that's not what's actually in the files, and git add -e needs to work on what's in the files, not some cleaned-up presentation thereof.


1What's in the files is machine-readable JSON. The result of the nbdime tools appears to be yaml. If Git had a native JSON diff engine, git add -p and company would be able to deal with the result, but Git doesn't, so it isn't. If Jupyter-notebooks used yaml, Git's line-oriented tools would be able to deal with them, but Jupyter-notebooks doesn't, so it isn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文