简体   繁体   English

.gitignore 忽略所有文件然后递归允许 *.foo

[英].gitignore ignore all files then recursively allow *.foo

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我想忽略我的存储库下文件夹中的所有内容,除了带有 *.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) (如果有人想知道如何证明这是合理的 - 我实际上正在为我所有的“逻辑”项目制作一个 git 存储库 - mac 上的音乐软件 - 但我只想存储实际的项目文件 *.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将此粘贴到 .gitignore

# Ignore all
/*

# But not these files...
!.gitignore
!*.foo

git status状态

And the only untracked file is .gitignore唯一未跟踪的文件是 .gitignore

if I typed 'git add .'如果我输入“git add”。 - no change, only .gitignore is seen and my 2 files are ignored. - 没有变化,只看到 .gitignore 并且我的 2 个文件被忽略。

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.这是我从中获取 .gitignore 文件的极其相似的问题。 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我正在使用 git --version 1.7.7 (也尝试过 1.7.3) - .gitignore 忽略所有文件,然后递归地允许某种类型的文件

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.您的问题是开头的/*模式匹配顶级的所有文件和目录 - 包括testdir ,因此testdir中的所有内容都被忽略。

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 .当您执行git add . with this config, you should find you have only .gitignore and *.foo files listed as changes to be committed.使用此配置,您应该会发现只有.gitignore*.foo文件被列为要提交的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM