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?

3 Answers 3

199

Just add a pattern to .gitignore

.*
!/.gitignore

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

5
  • 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 5, 2011 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. Nov 5, 2011 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)? Feb 12, 2020 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. Feb 13, 2020 at 17:08
  • if you want just hidden files within root user just use this line /.*
    – YGautomo
    Jul 31 at 6: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)" `
1
  • 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. May 2, 2020 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.