Search code examples
gitgitignore

Including only .gitignore, .bashrc, .vimrc, .vim/ in a Git repo


I thought I was pretty good with .gitignore files until just now. I'm trying to include only the following files in a Git repo:

  • .gitignore
  • .bashrc
  • .vimrc
  • .vim/ (everything in this directory)

I have read several other answers on StackOverflow (How do I ignore files in a directory in Git?, Git ignore sub folders and Is there a way to tell git to only include certain files instead of ignoring certain files?). But, I can't seem to get my .gitignore file right. Here's what I have in my .gitignore file presently:

*
.*
!.gitignore
!.bashrc*
!.vimrc
!.vim/

After I run git init; git add .; git status the repo contains:

$ git init; git add .; git status
Initialized empty Git repository in /home/username/.git/
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .bashrc
    new file:   .gitignore
    new file:   .vimrc

I've tried variations on the last line of my .gitignore file, but to no avail. Can anyone spot my mistake?

EDIT

I'm trying to find a solution which modifies only the .gitignore file.


Solution

  • You need to ignore files only (* ignores files and folder)

    **
    .**
    

    Then you can whitelist folders and their files:

    !.vim/
    !.vim/**
    

    Double-check what .gitignore rule applies with:

    git check-ignore -v .vim/afile
    

    What you want is for check-ignore to return nothing.