Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore - Ignore everything in a directory except one file

Tags:

gitignore

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?

like image 599
Radoslav Stoyanov Avatar asked Mar 24 '16 15:03

Radoslav Stoyanov


People also ask

Can Gitignore ignore a folder?

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

Does .gitignore work in subdirectories?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

How do I Gitignore everything?

You want to use /* instead of * or */ in most cases The above code would ignore all files except for . gitignore , README.md , folder/a/file. txt , folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders.

How do I ignore one file from a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.


3 Answers

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 
like image 161
oshell Avatar answered Sep 20 '22 09:09

oshell


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

like image 29
Daniel Artola Avatar answered Sep 21 '22 09:09

Daniel Artola


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.

like image 32
funder7 Avatar answered Sep 21 '22 09:09

funder7