Reputation: 26679
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
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
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
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
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
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
Reputation: 20212
I do it like this if I compare two branches:
git diff HEAD branchX path/to/my/folder
Upvotes: 0
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 optionrelative
Signed-off-by: Laurent Arnoud
Acked-by: Đoàn Trần Công DanhThe
diff.relative
boolean option set totrue
shows only changes in the current directory/value specified by thepath
argument of therelative
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 askedReported-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 intoNULL
pointer dereference inrun_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
Reputation: 3056
If you have a diff tool installed and configured (ex: kdiff3)
git difftool -d branch1 branch2 /path/to/dir/
Upvotes: 2
Reputation: 18869
To use Beyond Compare as the difftool for directory diff, remember enable follow symbolic links like so:
In a Folder Compare View → Rules (Referee Icon):
And then, enable follow symbolic links and update session defaults:
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
Reputation: 96777
Provide a path (myfolder in this case) and just run:
git diff myfolder/
Upvotes: 199