Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff doesn't ignore specified files in .gitignore

Tags:

git

batch-file

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?

like image 866
user2554585 Avatar asked Jul 23 '13 20:07

user2554585


People also ask

Why git ignore is not ignoring files?

gitignore only ignores files that are not part of the repository yet. If you already git add ed some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them.

How do I ignore certain files in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I ignore a .gitignore class file?

The easiest and most common way to ignore files is to use a gitignore file. Simply create a file named . gitignore in the repository's root directory. Then, add names and patterns for any files and directories that should not be added to the repository.

Should .gitignore be ignored?

The . gitignore file's purpose is to prevent everyone who collaborates on a project from accidentally commiting some common files in a project, such as generated cache files. Therefore you should not ignore .


1 Answers

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

like image 175
usumoio Avatar answered Sep 17 '22 11:09

usumoio