(git) diff output relative path?

我与影子孤独终老i 提交于 2019-12-08 15:26:54

问题


I need to get some diffs in my repo that are not relative to the base of the repo, but instead relative to a given base or given path.

By default I get:

git diff
diff --git a/path/to/file b/path/to/file
index 0cc125e..9bf911e 100644
--- a/path/to/file
+++ b/path/to/file

But what I want is something like:

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

I have looked over the --relative option (not what I am looking for), the --src/dst-prefix (these can only change the "a" or "b" parts. Am I missing something basic?


回答1:


Seems like --src-prefix and --dst-prefix are what you're asking for:

$ 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
  *

(so far, pretty standard; now:)

$ 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
  *

You can combine this with --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
  *
$ 



回答2:


git diff prints paths (of changed files) from the root of the repo - no matter where you are when executing the command.

git diff --relative will print paths from the dir you are in.

So if you need paths not starting from the repo-root move down (cd) to the directory (within your repo tree) where you with your paths to start from.



来源:https://stackoverflow.com/questions/23000086/git-diff-output-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!