37

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?

user318904
  • 2,968
  • 4
  • 28
  • 37

2 Answers2

39

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
luisa rosi
  • 500
  • 4
  • 5
  • 4
    `--relative` was exactly what I needed when I was using `git diff --name-only`, thanks – Lilás Dec 28 '17 at 13:03
  • 16
    `--relative` excludes changes outside the current directory. – Andy Stewart Jun 18 '20 at 07:56
  • You can make relative paths the default with `git config --global diff.relative true` for all your repositories; drop the `--global` to only use it for the current repository. – Robert May 11 '22 at 19:27
  • @Andy Stewart Yep, it even excludes them if you explicitly give it a directory outside the current one, e.g. with `..` for the parent or `:/` for the worktree root. – ak2 Nov 08 '22 at 10:30
20

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
  *
$ 
torek
  • 448,244
  • 59
  • 642
  • 775
  • I think this is correct, I never thought to try both of them of course..I hate porting patches. – user318904 May 06 '14 at 20:55
  • 1
    You seem to have an in-depth knowledge of git (I read some your answers). Do you have some resources to recommend? – Antarctica Jan 22 '21 at 17:00
  • 3
    @Antarctica: I've been using (and recently, contributing slightly to) Git for well over a decade. I don't have any single thing I'd specifically *recommend* at this point, but if you're looking for a decent book on Git, the [Pro Git book](https://git-scm.com/book/en/v2) has a bunch of plus-es: it's on line and kept up to date, it's free, and it's correct (unlike far too many online tutorials). There is also [Think Like (a) Git](http://think-like-a-git.net/), which covers most of what's missing from Pro Git. – torek Jan 22 '21 at 19:12