I'm trying to adapt the answers from Exclude file from "git diff" for the --stat
flag and failing. The accepted answer (create a driver) seems Unix-only (redirect to /bin/true
, whatever this means), plus it creates a driver and assigns it to the file kind of permanently; while I am looking for a switch to temporarily disable the diff for a file (or rather some files).
The scripting solution:
git diff `git status -s |grep -v ^\ D |grep -v file/to/exclude.txt |cut -b4-`
actually calls git status
and edits its output—while what I want is to instruct git diff
itself to ignore some files while calculating the simple --stat
(just lines changed). I went through the git-diff
docs, but can't seem to find such an option. Anyone give me a hand ?
$ git --version
git version 2.6.1.windows.1
The . gitignore file is a text file that tells Git which files or folders to ignore in a project. A local . gitignore file is usually placed in the root directory of a project. You can also create a global .
To exclude the files we need to follow below syntax: Use shorthand method. git diff -- . ':! FILENAME' Let’s check some below examples for reference. We can exclude the specific file with the git diff -- . ': (exclude)FILENAME. See the below example for reference in which I have excluded the single.php file from git diff.
The <paths> parameters, when given, are used to limit the diff to the named paths (you can give directory names and get diff for all files under them). The raw output format from "git-diff-index", "git-diff-tree", "git-diff-files" and "git diff --raw" are very similar. These commands all compare two sets of things; what is compared differs:
git diff [<options>] [--] [<path>… ] This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t.
git-diff-files [<pattern>… ] compares the index and the files on the filesystem. The "git-diff-tree" command begins its output by printing the hash of what is being compared. After that, all the commands print one output line per changed file. An output line is formatted this way:
The exclude pathspec trick, described in Making 'git log' ignore changes for certain paths, works here:
git diff --stat -- . ':(exclude)file/to/exclude.txt'
or, if you are in a subdirectory:
git diff --stat -- :/ ':(exclude,top)file/to/exclude.txt'
The latter can be spelled in various ways. For instance, this also works:
git diff --stat ':(top)' :!/file/to/exclude.txt
as does:
git diff --stat :/: :!/:file/to/exclude.txt
These are described in the gitglossary
documentation under the "pathspec" section. Note that the exclude feature is new in Git version 1.9 (and slightly broken until 1.9.2). The leading /
is an alias for top
and the !
is an alias for exclude
, with the long forms requiring the parentheses. The trailing colon before the actual pathname is optional when using the single-character aliases but forbidden when using the parentheses (this rule trips me up every time—I keep wanting to use :(exclude):...
rather than :(exclude)...
). The single quotes around the (top)
and (exclude)
pathspec components above are to protect the parentheses from being interpreted by the (Unix/Linux) shells; the Windows shell may have different ideas about which characters need protection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With