21

I'm new to git and I have no idea what I'm doing wrong. I want to run git diff --name-only but I want to exclude files ending in .test meaning that even if these haves have changed, I don't want git diff to output that it's changed. I tried to add these file to the .gitignore files but all that did is include .gitignore when I run the git diff command!

What am I doing wrong and how do I exclude files when I run git diff?

user2554585
  • 3,489
  • 6
  • 20
  • 23

1 Answers1

26

Okay, This is what you did wrong. Git will continue to track those files because you already started tracking them earlier in your project. At some point earlier did you run an initial commit that looked like:

git add . # this added everything to the tracking system including your .test files
git commit -a -m 'I just committed everything in my project .test files included'

Putting things is your gitignore file will keep future files that you create which end in .test from entering your project, but you need to remove the .test files you already told git to track from git's memory. Putting things in gitignore does not preform any action for already tracked files. What you need to do now is this:

Option 1:

# you can remove the files from gits tracking system and then put them back
# when you go to put them back now git will have no memory of tracking these
# and will consider future edits to be ignored
# Back your .test files up before doing this, and this will delete them from your project
git rm /path/to/your/file.test

Option 2:

# this is safer but does not use gitignore at all
git update-index --assume-unchanged /path/to/your/file.test

When you run option 2 you are telling git that for the rest of time that file is never changing (even when it does in real life) This let you keep your .test files as part of your tracked project (as they are now), but git will never bother you about changes to them ever again. Note that this operation can be undone at any time and is not destructive, which is why it is safer. Also you should go read up on it before you use it.

https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html

matt
  • 515,959
  • 87
  • 875
  • 1,141
usumoio
  • 3,500
  • 6
  • 31
  • 57