35

I know there is a lot of questions like this, but no one solved my problem. I want something very simple - ignore all files and folders under specific folder except one file. This is what I try:

#Ignore
public/typings/*

#Allow
!public/typings/browser/ambient/jquery/jquery.d.ts

...but the file is still ignored.

Any suggestions?

Radoslav Stoyanov
  • 1,460
  • 5
  • 27
  • 40

3 Answers3

47

It seems ! only works if the file is in the same folder. A possible workaround would be to nest the same statement till you get to your final file. A bit messy, but it works.

public/typings/*
!public/typings/browser
public/typings/browser/*
!public/typings/browser/ambient
public/typings/browser/ambient/*
!public/typings/browser/ambient/jquery
public/typings/browser/ambient/jquery/*
!public/typings/browser/ambient/jquery/jquery.d.ts
oshell
  • 8,923
  • 1
  • 29
  • 47
5

If the file was added in a previous commit before you put it in git ignore, it's possible your .gitignore is not working. I had the same problem few days ago. I solved the problem thanks to this post:

Post int randallkent

I hope this helps you

EDIT:

solution was found here: gitignore directory exception not working

Community
  • 1
  • 1
Daniel Artola
  • 329
  • 3
  • 10
  • 1
    and did you read this too? http://stackoverflow.com/questions/16678936/gitignore-directory-exception-not-working If this doesn't works, I'm afraid I have not more solutions. Sorry man – Daniel Artola Mar 24 '16 at 15:31
  • Ooooh.... thank you so much! This helped. Single file solution looks a little bit strange but at least works :) – Radoslav Stoyanov Mar 24 '16 at 15:41
  • @DanieloFair so the first part of your answer is not necessary? just to do what's indicated in your second link? – m4l490n Aug 31 '17 at 18:46
3

I've been struggling a lot with this subject, let me share a procedure that worked for me:

1) Add the top level folder "abc/" to your .gitignore 2) Let's say that in "abc/" you have also "def/" and "jkl/" folders. If you want to keep the contents of "jkl/" you can just do:

git add -f jkl/*.php

Ok! Now everything inside "jkl/" with .php extension will be tracked by git, even in the next commits, without any headaches!

I've found this to be the right solution for my case, because I was really going insane trying to understand how gitignore does scan files and directories.

funder7
  • 1,622
  • 17
  • 30