1

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.

MikeyE
  • 1,756
  • 1
  • 18
  • 37
  • 1
    No, my `.vim/` directory contains one sub-directory named `bundle`. There a lot of files and directories under the `bundle` folder. What do you mean by "the output seems correct"? My goal is to include the `.vim/` folder and everything in it. – MikeyE Dec 06 '17 at 03:06
  • You can use `-force` flag : `git add -f .vim` – RC_02 Dec 06 '17 at 03:30
  • I tried `!/.vim` with the same results. I could use the `-force` flag, except I'm trying to find a solution that works by modifying only the `.gitignore` file. I'll update the question to reflect this. – MikeyE Dec 06 '17 at 03:57

1 Answers1

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I tried your suggestion, and it didn't add the `.vim/` folder. It only added `.gitignore`, `.bashrc` and `.vimrc`. I first did `rm -rf ~/.git/`, then edited `.gitignore` using `**` and `.**`. I then ran `git init; git add .; git status` to view the files added to the repo. – MikeyE Dec 06 '17 at 14:26
  • @MikeyE Try `git check-ignore -v .vim/afile` to see which ignore rule is applied. And I have edited the answer. – VonC Dec 06 '17 at 15:55
  • Your comments about `**` ignoring files and `*` ignore files and folders cleared it up for me. I've selected your answer as the accepted answer. – MikeyE Dec 17 '17 at 20:50