20

I'm trying to make git ignore some of my files and I found one description about how you could do this

From: http://github.com/guides/git-cheat-sheet TO IGNORE SOME FILES

Add a file in the root directory called .gitignore and add some files to it: (comments begin with hash) *.log db/schema.rb db/schema.sql

Git automatically ignores empty directories. If you want to have a log/ directory, but want to ignore all the files in it, add the following lines to the root .gitignore: (lines beginning with ‘!’ are exceptions)

log/* !.gitignore

Then add an empty .gitignore in the empty directory:

touch log/.gitignore

So I made a file called .gitignore in my folder for my project and wrote the following in it:

phpMyAdmin/*
nbproject/*
inc/mysql_config.php
!.gitignore

But when I commit, the files isent excluded from the commit...

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Victor Bjelkholm
  • 2,177
  • 9
  • 28
  • 50
  • Are none of the files being excluded, or are some being excluded and others aren't? If you provide a little more detail about what're seeing and what you're expecting (including file paths) that would be helpful. – Guildencrantz Jul 10 '10 at 20:14
  • possible duplicate of [Git: Remove a file from the repository without deleting it from the local filesystem](http://stackoverflow.com/questions/1143796/git-remove-a-file-from-the-repository-without-deleting-it-from-the-local-filesy) – Cascabel Feb 01 '12 at 20:31
  • Be carefull with that netbeans ignore... `nbproject should be checked into the version control system. nbproject contains project metadata that enables others users to open the project in NetBeans without having to import the project first.` [source](http://netbeans.org/kb/docs/java/import-eclipse.html#versioning) – Ron van der Heijden Jan 31 '13 at 23:30

2 Answers2

33

https://www.gitignore.io/

I created a web utility that can help you generate useful .gitignore files for your project. It will help you ignore operating system, programming language, and IDE generated files.

Joe
  • 1,320
  • 13
  • 11
29

According to man gitignore:

DESCRIPTION

A gitignore file specifies intentionally untracked files that git should ignore. Note that all the gitignore files really concern only files that are not already tracked by git; in order to ignore uncommitted changes in already tracked files, please refer to the git update-index --assume-unchanged documentation.

So it doesn't help if you've already added them. It's mostly for preventing the addition in the first place. That way, you can ignore .tmp files and add a whole directory without worrying that you'll add the .tmp files.

I believe you can remove them from the index with:

git rm --cached file_to_stop_tracking_but_dont_want_to_delete.txt

Update:

Also, the .gitignore needs to be at the base directory or at least above where those directories are. Also, take the "*" out of the directories:

phpMyAdmin/
nbproject/
inc/mysql_config.php
!.gitignore

And be careful of phpMyAdmin/ vs /phpMyAdmin vs phpMyAdmin. Also from man gitignore:

  • If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).

  • If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname without leading directories.

  • Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, Documentation/*.html matches Documentation/git.html but not Documentation/ppc/ppc.html. A leading slash matches the beginning of the pathname; for example, /*.c matches cat-file.c but not mozilla-sha1/sha1.c.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
eruciform
  • 7,680
  • 1
  • 35
  • 47