Excluding files from git-diff

Git

Git Problem Overview


I am tracking a project with git. There are some Xcode project files in the working copy that I want to keep tracking, but do not want to see in diffs, because there are always dozens of changed lines that I am never interested in. Is there a simple way to have git-diff skip these files? I’ve tried to set up a custom “silent” diff tool:

$ cat .gitattributes
Project.xcodeproj/* diff=nodiff

$ cat ~/.gitconfig [diff "nodiff"] command = /bin/true

But:

$ git diff
external diff died, stopping at Project.xcodeproj/zoul.mode1v3.

What am I doing wrong?

Git Solutions


Solution 1 - Git

The problem is that /bin/true will return immediately without reading its input. git diff therefore thinks, quite reasonably, that it has died prematurely.

What you really want to do is to unset the diff attribute, not set it to a bogus command. Try this in your .gitattributes:

Project.xcodeproj/* -diff

Solution 2 - Git

You may use an alias in your .git/config

[alias]
        mydiff = !git diff | filterdiff -x "*/Project.xcodeproj/*"

You need filterdiff (from patchutils) for this trick.

sudo apt-get install patchutils

Still the diff isn't perfect, it leaves some garbage :

yannick@yannick-desktop:~/git-filter-test$ git mydiff
diff --git a/Project.xcodeproj/dummy.txt b/Project.xcodeproj/dummy.txt
index 3e1f9e6..89dfed9 100644
diff --git a/dummy2.txt b/dummy2.txt
index 91966ce..d9588a9 100644
--- a/titi.txt
+++ b/titi.txt
@@ -1,3 +1,3 @@
 aaaaaaaaaa
-bbbbbbbbb
 cccccc
+ddd

Solution 3 - Git

Another solution that produces clean output without any external tools (add to .git/config):

[alias]
	mydiff = !git diff -- $(git diff --name-only | grep -Ev "Project.xcodeproj/")

Then run it with:

git mydiff

Note that git diff --name-only is better than git ls-files because it will pass a shorter list of files to git diff, since only files that are modified will be included. I've run into trouble with exceeding the maximum number of arguments in large projects when using git ls-files.

Solution 4 - Git

Just incase someone else has the same pain we had. We wanted to exclude a file that had already been committed.

This post was way more useful: https://stackoverflow.com/questions/1158857/working-with-git-info-exclude-too-late

Specifically what you need to ignore a file is actually use the command git remove See git rm (http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

you test it by going

git rm --dry-run *.log
(if you say wanted to exclude all the log files)

this will output what would be excluded if you ran it.

then

you run it by going

git rm *.log
(or whatever filename path / expression you want to)

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
QuestionzoulView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GityanjostView Answer on Stackoverflow
Solution 3 - GitConnor McKayView Answer on Stackoverflow
Solution 4 - GitEvolveView Answer on Stackoverflow