150

I'd like to have Git ignore all hidden files and directories. i.e.

  • .aptitude
  • .ssh/
  • .bash_rc
  • config/.hidden

Is there a simple rule to cover this without specifically adding each entry?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Poe
  • 2,971
  • 6
  • 30
  • 38

3 Answers3

199

Just add a pattern to .gitignore

.*
!/.gitignore

Edit: Added the .gitignore file itself (matters if it is not yet commited).

Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46
  • 3
    You might want to force-add some files that are necessary after this. eg. the .htaccess file. Based on your requirements of course. – dakdad Nov 05 '11 at 16:20
  • 3
    @dakdad: Thanks for the suggestion. Improved the answer. If Poe has some special files like `.htaccess` already checked in they keep being followed. `gitignore` is only important for new files. – Daniel Böhmer Nov 05 '11 at 16:35
  • @DanielBöhmer Maybe this is getting off the subject, but is there a reason for un-ignoring only `/.gitignore` and not every `.gitignore` file regardless of where it is located? I.e. why not list `!.gitignore` (no slash)? – Jason Young Feb 12 '20 at 16:08
  • 1
    @JasonYoung Interesting question. Well, this pattern un-ignores just the file itself. I find this more concise with no side effects for an example to copy & paste. Of course, you can probably just un-ignore any `.gitignore` files in your top-level `.gitignore` file. But that choice is up to you. – Daniel Böhmer Feb 13 '20 at 17:08
  • if you want just hidden files within root user just use this line `/.*` – YGautomo Jul 31 '23 at 06:01
77

.gitignore will only effect files that haven't been 'added' already.

To make new .gitignore entries affect all files

  1. Make changes to .gitignore
  2. git commit -a -m "Pre .gitignore changes"
  3. git rm -r --cached .
  4. git add .
  5. git commit -a -m "Post .gitignore changes"
  6. git status should output "nothing to commit (working directory clean)" `
Nat Darke
  • 871
  • 7
  • 3
  • 1
    The line "haven't been 'added' already" is the key here, because if you already commited a given file the ignore will not work on him anymore you must delete it from the repository and the next time you create it the .gitignore will ignore it. – Georgi Peev May 02 '20 at 10:16
26

In .git/info/exclude, add this line:

.*

This will make ignoring all hidden/dot files recursively the default for every repository on the machine. A separate .gitignore file for every repo is not needed this way.

dessert
  • 248
  • 6
  • 15
slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123