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

Git

Git Problem Overview


git diff actually runs a diff on all source code. How do I do this on a certain directory, so that I can view modifications on files underneath it?

Git Solutions


Solution 1 - Git

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

git diff myfolder/

Solution 2 - Git

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/

Solution 3 - Git

What I was looking for was this:

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

Solution 4 - Git

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.

Solution 5 - Git

Add Beyond Compare as your difftool in Git and add an alias for diffdir as:

git config --global alias.diffdir = "difftool --dir-diff --tool=bc3 --no-prompt"

Get the gitdiff as:

git diffdir 4bc7ba80edf6  7f566710c7

Reference: Compare entire directories w git difftool + Beyond Compare

Solution 6 - Git

You should make a habit of looking at the documentation for stuff like this. It's very useful and will improve your skills very quickly. Here's the relevant bit when you do git help diff

   git diff [options] [--no-index] [--] <path> <path>

The two <path>s are what you need to change to the directories in question.

Solution 7 - Git

To use Beyond Compare as the difftool for directory diff, remember enable follow symbolic links like so:

In a Folder Compare ViewRules (Referee Icon):

Enter image description here

And then, enable follow symbolic links and update session defaults:

Enter image description here


OR,

set up the alias like so:

git config --global alias.diffdir "difftool --dir-diff --tool=bc3 --no-prompt --no-symlinks"

Note that in either case, any edits made to the side (left or right) that refers to the current working tree are preserved.

Solution 8 - Git

If you want to exclude the sub-directories, you can use

git diff <ref1>..<ref2> -- $(git diff <ref1>..<ref2> --name-only | grep -v /)

Solution 9 - Git

If you have a diff tool installed and configured (ex: kdiff3)

git difftool -d  branch1 branch2 /path/to/dir/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpriyaView Question on Stackoverflow
Solution 1 - GitGeoView Answer on Stackoverflow
Solution 2 - GitbcattleView Answer on Stackoverflow
Solution 3 - GitAmin KarbasView Answer on Stackoverflow
Solution 4 - GitVonCView Answer on Stackoverflow
Solution 5 - GitBaraniView Answer on Stackoverflow
Solution 6 - GitNoufal IbrahimView Answer on Stackoverflow
Solution 7 - GitAshutosh JindalView Answer on Stackoverflow
Solution 8 - GitSalvian ReynaldiView Answer on Stackoverflow
Solution 9 - GitKhogaEslamView Answer on Stackoverflow