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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8024924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:15:08  来源:igfitidea点击:

.gitignore ignore all files then recursively allow *.foo

git

提问by PandaWood

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 .' - no change, only .gitignore is seen and my 2 files are ignored.

如果我输入“git add”。- 没有变化,只看到 .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. 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

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

回答by camh

Your problem is that the /*pattern at the beginning is matching all files and directories at the top level - including testdir, so everything inside testdiris 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 .with this config, you should find you have only .gitignoreand *.foofiles listed as changes to be committed.

当你git add .使用这个配置时,你会发现你只有.gitignore*.foo文件被列为要提交的更改。