git diff command to exclude some folders and files
Asked Answered
B

3

7

I am currently using the following Git diff command to get the list of files changes. I want to exclude certain folders (say datafiles) and files(.DSstore). Is there a way to do this?

git diff --no-index /Users/USERNAME/Documents/FWintegration/ingo2/ukon_4355c1 /Users/USERNAME/Documents/FWintegration/ingo2mirror/ingo2mirror
Benyamin answered 25/1, 2018 at 1:18 Comment(8)
And...I just realized both my answers are stupid with the --noindex option.Highpowered
Are you looking for a list of files that have changed? git diff isn't a good tool for that. git log and, at a much lower level, git rev-list are better tools.Symphonious
Although git diff --stat might be helpful, depending on your exact requirements.Symphonious
@PaulHicks I'm not sure git is a good tool at all in this situation, now that I've looked at "--no-index". Maybe some git expert will come along and school me. I certainly am not one.Highpowered
@PaulHicks & zzxyz- why do you say git is not a good tool here ?also what is wrong with --no-indexBenyamin
It is diffing two files where at least one of them is not in version control. find+diff / ack / ag and other tools like that might be more suitable.Symphonious
@user3682248 - --no-index is explicitly asking a version control system to not use version control information--if I understand the documentation correctly. I'd compare it to using Netbeans or Visual Studio to edit text files. Nothing really wrong with it, but there might be a better tool for the job.Highpowered
@all - My exact requirement is I have two folders on a Mac ..want to compare them excluding some folder and want to know what all files have changes, as a followup I would like to copy some of the changed files ,is there a command-line utility which can help me do this?Benyamin
P
5
git diff -- ':!unwantedpath'

For example, git diff -- ':!foo/bar/datafiles' ':!*.DSstore'.

But it is a relatively new feature, so it works only if you are using a new version of Git. I don't remember in which version it was introduced, but it's some version after 2.10.0.

Update

The patterns like ':!unwantedpath' are pathspec.

Pyle answered 25/1, 2018 at 4:51 Comment(3)
This is much better than my answer, but not nearly as hilarious.Highpowered
Can you elaborate? E.g., what is it supposed to do? Is ":!" a shorthand for something? Does the feature have a name? Does it support regular expressions or not? Or only wild cards? Or not even wild cards? Is it relative paths? Relative to what? Relative to the Git repository root? Is ".." supported? What about spaces in folder names? - How are spaces escaped and/or handled? What about other special characters? "*"? How are characters with special meanings themselves escaped (if any)? Etc.Drakensberg
@PeterMortensen It's pathspec.Pyle
H
3

Unfortunately, this is the most complicated, but it actually works with --no-index:

git diff | awk '{if (/^diff --git/) {if (/d1\/\S*$/){display=0}else{display=1}};if (display==1) {print}}'

Each file diff git diff performs starts with "diff --git". So...we have AWK look for that line. If it finds it, and then doesn't like it, it stops printing until it finds a diff --git it likes.

So, the only part of the AWK statement you modify is if (/d1\/\S*$/) This is how it filters...if this regular expression matches, it'll ignore this diff—not print it). This filters out "d1/" followed by any number of non-whitespace characters up until the end of the line (so it's looking at the 2nd filename only and not the first). For your example, you'd want the if to be:

if (/datafiles\/\S*$/ || /\.DSstore$/)

If this is too insane for you, I'd highly recommend checking out Beyond Compare which is a crazy powerful directory/file comparison tool with a nice UI.

Highpowered answered 25/1, 2018 at 3:7 Comment(1)
Yes! This actually works with --no-index. Sadly, it removes the color coding from the git diff lines. Any idea how to preserve that?Praseodymium
G
0

Simply by using the --exclude argument. You can ignore folders / files with exact names or even having a regex pattern using the wild card operator *.

Example:

diff -r /folder1/ /folder2/  --exclude="*.iml" --exclude=".idea" --exclude=".git" --exclude="target" --exclude=".DS_Store"
Gilliangilliard answered 19/2 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.