(git)diff输出相对路径?

use*_*904 11 git diff patch git-diff difference

我需要在我的仓库中获得一些与仓库基础无关的差异,而是相对于给定的基础或给定路径.

我默认得到:

git diff
diff --git a/path/to/file b/path/to/file
index 0cc125e..9bf911e 100644
--- a/path/to/file
+++ b/path/to/file
Run Code Online (Sandbox Code Playgroud)

但我想要的是:

git diff --prefix=/new/path/to
diff --git a/new/path/to/file b/new/path/to/file
index 0cc125e..9bf911e 100644
--- a/new/path/to/file
+++ b/new/path/to/file
Run Code Online (Sandbox Code Playgroud)

我查看了--relative选项(不是我要找的), - src/dst-prefix(这些只能改变"a"或"b"部分.我错过了一些基本的东西吗?

tor*_*rek 10

好像--src-prefix--dst-prefix 你问了:

$ cd .../git/builtin
$ ed - var.c << end
> 0a
> xxx
> .
> wq
> end
$ git diff
diff --git a/builtin/var.c b/builtin/var.c
index aedbb53..5210013 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -1,3 +1,4 @@
+xxx
 /*
  * GIT - The information manager from hell
  *
Run Code Online (Sandbox Code Playgroud)

(到目前为止,非常标准;现在:)

$ git diff --src-prefix=a/new/ --dst-prefix=b/new/
diff --git a/new/builtin/var.c b/new/builtin/var.c
index aedbb53..5210013 100644
--- a/new/builtin/var.c
+++ b/new/builtin/var.c
@@ -1,3 +1,4 @@
+xxx
 /*
  * GIT - The information manager from hell
  *
Run Code Online (Sandbox Code Playgroud)

你可以将它与--relative:

$ git diff --relative --src-prefix=a/new/ --dst-prefix=b/new/
diff --git a/new/var.c b/new/var.c
index aedbb53..5210013 100644
--- a/new/var.c
+++ b/new/var.c
@@ -1,3 +1,4 @@
+xxx
 /*
  * GIT - The information manager from hell
  *
$ 
Run Code Online (Sandbox Code Playgroud)

  • @Antarctica:十多年来我一直在使用 Git(最近,对 Git 做出了一些贡献)。目前我没有任何特别“推荐”的东西,但如果您正在寻找一本关于 Git 的不错的书,[Pro Git 书](https://git-scm.com/book /en/v2)有很多优点:它在线并且保持最新,它是免费的,而且它是正确的(与太多的在线教程不同)。还有 [Think Like (a) Git](http://think-like-a-git.net/),它涵盖了 Pro Git 所缺少的大部分内容。 (3认同)

小智 9

git diff 从存储库的根目录打印(已更改文件的)路径-无论执行命令时在何处。

git diff --relative 将从您所在的目录中打印路径。

因此,如果您需要的路径不是从repo根目录开始,则向下(cd)移至您要从其开始路径的目录(在repo树内)。

  • `--relative` 排除当前目录之外的更改。 (6认同)
  • `--relative` 正是我在使用 `git diff --name-only` 时所需要的,谢谢 (2认同)