priya
priya

Reputation: 26679

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

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?

Upvotes: 224

Views: 209840

Answers (11)

R...
R...

Reputation: 2570

If it is only one directory you are interested in, the easiest way is to navigate to that directory in your terminal and then do diff with other branch.

E.g., to compare the current directory of the current branch with the same directory in the master branch

inside the directory you are interested in, do :

git diff master *

Or if you are interested in the file names only add name-only option:

git diff master --name-only *

If it's a remote branch that you want to compare with, make sure you have fetched it first then do the diff, e.g.,

git fetch origin master
git diff origin/master --name-only *

Upvotes: 3

Amin
Amin

Reputation: 643

I was looking for this:

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

Upvotes: 37

Salvian Reynaldi
Salvian Reynaldi

Reputation: 102

If you want to exclude the subdirectories, you can use

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

Upvotes: 2

Barani
Barani

Reputation: 57

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

Upvotes: 4

bcattle
bcattle

Reputation: 12819

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/

Upvotes: 93

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72745

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.

Upvotes: 2

Black
Black

Reputation: 20212

I do it like this if I compare two branches:

git diff HEAD branchX path/to/my/folder

Upvotes: 0

VonC
VonC

Reputation: 1323753

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.

Upvotes: 22

KhogaEslam
KhogaEslam

Reputation: 3056

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

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

Upvotes: 2

Ashutosh Jindal
Ashutosh Jindal

Reputation: 18869

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.

Upvotes: 1

Geo
Geo

Reputation: 96777

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

git diff myfolder/

Upvotes: 199

Related Questions