Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore ignore all files then recursively allow *.foo

Tags:

git

There's already several questions similar to this, but none of the answers work for me.

I want to ignore everything in the folders below my repository except files with *.foo

(If anyone is wondering how this can be justified - I'm actually making a git repository for all my "Logic" projects - music software on the mac - but I only want to store the actual project files *.logic)

I'm going to spell it out, so we're all on the same plate. Here's what I do, starting from scratch:

Setup:

mkdir temp cd temp mkdir testdir cd testdir touch include.foo touch dontinclude.bad cd.. git init touch .gitignore 

Paste this in to .gitignore

# Ignore all /*  # But not these files... !.gitignore !*.foo 

git status

And the only untracked file is .gitignore

if I typed 'git add .' - no change, only .gitignore is seen and my 2 files are ignored.

Why doesn't this work and how can you change the procedure above to make it work?

Here's the extremely similar question where I got the .gitignore file from. I'm using git --version 1.7.7 (also tried 1.7.3) - .gitignore to ignore all files, then recursively allows files of a certain type

like image 663
PandaWood Avatar asked Nov 06 '11 02:11

PandaWood


People also ask

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 ignore a .gitignore file?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Will Gitignore remove files from repo?

You basically remove and re-add all files, but git add will ignore the ones in . gitignore . Using the --cached option will keep files in your filesystem, so you won't be removing files from your disk. Note: Some pointed out in the comments that you will lose the history of all your files.

Does Gitignore get cloned?

gitignore is created, the file will still be part of the repository and therefore cloned. Even that's an overstatement: an entry in a . gitignore file is only considered when some file is untracked. A committed file, as (because) it is being checked out, automatically becomes tracked.


1 Answers

Your problem is that the /* pattern at the beginning is matching all files and directories at the top level - including testdir, so everything inside testdir is ignored.

This is what you want:

# Ignore everything * # Don't ignore directories, so we can recurse into them !*/ # Don't ignore .gitignore and *.foo files !.gitignore !*.foo 

When you do a git add . with this config, you should find you have only .gitignore and *.foo files listed as changes to be committed.

like image 143
camh Avatar answered Sep 20 '22 15:09

camh