How do I 'git diff' on a certain directory?

Provide a path (myfolder in this case) and just run:

git diff myfolder/

Not only you can add a path, but you can add git diff --relative to get result relative to that folder.

git -C a/folder diff --relative

And with Git 2.28 (Q3 2020), the commands in the "diff" family learned to honor the "diff.relative" configuration variable.

See commit c28ded8 (22 May 2020) by Laurent Arnoud (spk).
(Merged by Junio C Hamano -- gitster -- in commit e34df9a, 02 Jun 2020)

diff: add config option relative

Signed-off-by: Laurent Arnoud
Acked-by: Đoàn Trần Công Danh

The diff.relative boolean option set to true shows only changes in the current directory/value specified by the path argument of the relative option and shows pathnames relative to the aforementioned directory.

Teach --no-relative to override earlier --relative

Add for git-format-patch(1) options documentation --relative and --no-relative

The documentation now includes:

diff.relative:

If set to 'true', 'git diff' does not show changes outside of the directory and show pathnames relative to the current directory.


Warning: Before Git 2.34 (Q4 2021), "git diff --relative"(man) segfaulted and/or produced incorrect result when there are unmerged paths.

See commit 8174627 (22 Aug 2021) by Đoàn Trần Công Danh (sgn).
(Merged by Junio C Hamano -- gitster -- in commit c8f4916, 08 Sep 2021)

diff-lib: ignore paths that are outside $cwd if --relative asked

Reported-by: Thomas De Zeeuw
Tested-by: Carlo Arenas
Signed-off-by: Đoàn Trần Công Danh

For diff family commands, we can tell them to exclude changes outside of some directories if --relative is requested.

In diff_unmerge(), NULL will be returned if the requested path is outside of the interesting directories, thus we'll run into NULL pointer dereference in run_diff_files when trying to dereference its return value.

Checking for return value of diff_unmerge before dereferencing is not sufficient, though.
Since, diff engine will try to work on such pathspec later.

Let's not run diff on those unintesting entries, instead.
As a side effect, by skipping like that, we can save some CPU cycles.


What I was looking for was this:

git diff <ref1>..<ref2> <dirname>

If you're comparing different branches, you need to use -- to separate a Git revision from a filesystem path. For example, with two local branches, master and bryan-working:

$ git diff master -- AFolderOfCode/ bryan-working -- AFolderOfCode/

Or from a local branch to a remote:

$ git diff master -- AFolderOfCode/ origin/master -- AFolderOfCode/

Tags:

Git