使 .gitignore 忽略除少数文件之外的所有内容

发布于 2024-07-23 08:22:45 字数 269 浏览 11 评论 0原文

据我所知,.gitignore 文件隐藏了 Git 版本控制中的指定文件。

如何告诉 .gitignore 忽略除我使用 Git 跟踪的文件之外的所有内容? 就像是:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

I understand that a .gitignore file cloaks specified files from Git's version control.

How do I tell .gitignore to ignore everything except the files I'm tracking with Git? Something like:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

狼性发作 2024-07-30 08:22:45

可选前缀!,用于否定模式; 排除的任何匹配文件
先前的模式将再次包含在内。 如果否定模式匹配,
这将覆盖优先级较低的模式源。

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# ...even if they are in subdirectories
!*/

An optional prefix ! which negates the pattern; any matching file excluded by
a previous pattern will become included again. If a negated pattern matches,
this will override lower precedence patterns sources.

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# ...even if they are in subdirectories
!*/
離人涙 2024-07-30 08:22:45

如果您想忽略目录中除其中一个文件之外的全部内容,您可以为文件路径中的每个目录编写一对规则。 例如 .gitignore 忽略 pippo 文件夹,除了 pippo/pluto/paperino.xml

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

请注意,/*是文件夹所必需的。 以下内容不会起作用:

folder
!folder/some-file.txt

但只有这个会起作用:

folder/*
!folder/some-file.txt

请注意,如果您只是在上面编写:

pippo/*
!pippo/pluto/paperino.xml

它将不起作用,因为 Git 不存在中间 pluto 文件夹,因此 paperino.xml 找不到存在的地方。

If you want to ignore the whole content of a directory except one file inside it, you could write a pair of rules for each directory in the file path. E.g. .gitignore to ignore the pippo folder except from pippo/pluto/paperino.xml

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

Note that the /* is required for the folders. The following will not work:

folder
!folder/some-file.txt

but only this will:

folder/*
!folder/some-file.txt

Note that if you simply had written above:

pippo/*
!pippo/pluto/paperino.xml

It wouldn't work because the intermediary pluto folder would not exist to Git, so paperino.xml could not find a place in which to exist.

泪冰清 2024-07-30 08:22:45

在大多数情况下,您希望使用 /* 而不是 **/

使用 * 有效,但它递归地工作。 从那时起它就不会再查看目录了。 人们建议再次使用 !*/ 来包含目录,但实际上最好使用 /* 将最高级别的文件夹列入黑名单

# Blocklist files/folders in same directory as the .gitignore file
/*

# Includelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log

# Includelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

# Adding to the above, this also works...
!/folder/a/deeply
/folder/a/deeply/*
!/folder/a/deeply/nested
/folder/a/deeply/nested/*
!/folder/a/deeply/nested/subfolder

上面的代码将忽略除 之外的所有文件.gitignoreREADME.mdfolder/a/file.txtfolder/a/b1//a/b2/ 以及最后两个文件夹中包含的所有内容。 (并且 .DS_Store*.log 文件在这些文件夹中将被忽略。)

显然我可以这样做,例如 !/folder!/.gitignore 也是如此。

更多信息:http://git-scm.com/docs/gitignore

You want to use /* instead of * or */ in most cases

Using * is valid, but it works recursively. It won't look into directories from then on out. People recommend using !*/ to includelist directories again, but it's actually better to blocklist the highest level folder with /*

# Blocklist files/folders in same directory as the .gitignore file
/*

# Includelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log

# Includelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

# Adding to the above, this also works...
!/folder/a/deeply
/folder/a/deeply/*
!/folder/a/deeply/nested
/folder/a/deeply/nested/*
!/folder/a/deeply/nested/subfolder

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. (And .DS_Store and *.log files would be ignored in those folders.)

Obviously I could do e.g. !/folder or !/.gitignore too.

More info: http://git-scm.com/docs/gitignore

猫瑾少女 2024-07-30 08:22:45

更具体一点:

示例:忽略 webroot/cache 中的所有内容 - 但保留 webroot/cache/.htaccess

请注意 cache 文件夹后面的斜杠 (/):

FAILS

webroot/cache*
!webroot/cache/.htaccess

WORKS

webroot/cache/*
!webroot/cache/.htaccess

A little more specific:

Example: Ignore everything in webroot/cache - but keep webroot/cache/.htaccess.

Notice the slash (/) after the cache folder:

FAILS

webroot/cache*
!webroot/cache/.htaccess

WORKS

webroot/cache/*
!webroot/cache/.htaccess
空城之時有危險 2024-07-30 08:22:45

让我们工具化吧!

正如@Joakim 所说,要忽略文件,您可以使用如下所示的内容。

# Ignore everything
*

# But not these files...
!.gitignore
!someFile.txt

但如果文件位于嵌套目录中,则编写这些规则有点困难。

例如,如果我们想跳过位于 aDir/anotherDir/someOtherDir/aDir/bDir/cDir 中的所有文件,但不跳过 a.txt
然后,我们的 .gitignore 将是这样的,

# Skip all files
*

# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

这很难用手写。

为了解决这个障碍,我创建了一个名为 git-do-not-ignore 的 Web 应用程序,它将为您生成规则。

工具

演示

示例输入

aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

示例输出

!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

Let's toolify!

As @Joakim said, to ignore a file, you can use something like below.

# Ignore everything
*

# But not these files...
!.gitignore
!someFile.txt

but if the file is in nested directories, it's a little difficult to write those rules.

For example, if we want to skip all files but not a.txt, located in aDir/anotherDir/someOtherDir/aDir/bDir/cDir.
Then, our .gitignore will be something like this

# Skip all files
*

# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

This is quite hard to write by hand.

To solve this hurdle, I've created a web app named git-do-not-ignore which will generate the rules for you.

Tool

Demo

Sample Input

aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

Sample Output

!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
锦欢 2024-07-30 08:22:45
# ignore these
*
# except foo
!foo
# ignore these
*
# except foo
!foo
时光暖心i 2024-07-30 08:22:45

与 OP 有类似的问题,但前 10 个赞成的答案都没有真正起作用
我终于发现了以下

错误语法:

*
!bin/script.sh

正确语法:

*
!bin
!bin/script.sh

来自gitignore 手册页

可选前缀“!” 这否定了模式; 先前模式排除的任何匹配文件将再次包含在内。 如果排除某个文件的父目录,则无法重新包含该文件。 出于性能原因,Git 不会列出排除的目录,因此包含文件上的任何模式都无效,无论它们是在哪里定义的。

这意味着上面的“错误语法”是错误的,因为 bin/script.sh 无法重新包含,因为 bin/ 被忽略。 就这样。

扩展示例:

$ 树 .

.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh

$ cat .gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh

$ git status --untracked-files --ignored

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        bin/sub/folder/path/script.sh

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        bin/ignore.txt
        bin/sub/folder/path/other.sh

nothing added to commit but untracked files present (use "git add" to track)

Had the similar issue as OP but none of top 10 upvoted answer actually worked.
I finally found out the following

Wrong syntax :

*
!bin/script.sh

Correct syntax :

*
!bin
!bin/script.sh

Explanation from gitignore man page :

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

Which means that "Wrong syntax" above is wrong because bin/script.sh cannot be reincluded as bin/ is ignored. That's all.

Extended example :

$ tree .

.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh

$ cat .gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh

$ git status --untracked-files --ignored

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        bin/sub/folder/path/script.sh

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        bin/ignore.txt
        bin/sub/folder/path/other.sh

nothing added to commit but untracked files present (use "git add" to track)
傲鸠 2024-07-30 08:22:45

要忽略目录中的某些文件,您必须按照正确的顺序执行此操作:

例如,忽略文件夹“application”中除 index.php 和文件夹“config”之外的所有内容注意顺序

你必须首先否定你想要的东西。

失败

application/*

!application/config/*

!application/index.php

有效

!application/config/*

!application/index.php

application/*

To ignore some files in a directory, you have to do this in the correct order:

For example, ignore everything in folder "application" except index.php and folder "config" pay attention to the order.

You must negate want you want first.

FAILS

application/*

!application/config/*

!application/index.php

WORKS

!application/config/*

!application/index.php

application/*

向日葵 2024-07-30 08:22:45

您可以使用 git config status.showUntrackedFiles no ,所有未跟踪的文件将对您隐藏。 有关详细信息,请参阅man git-config

You can use git config status.showUntrackedFiles no and all untracked files will be hidden from you. See man git-config for details.

前事休说 2024-07-30 08:22:45

这就是我保持文件夹结构而忽略其他所有内容的方法。 每个目录(或 .gitkeep)中必须有一个 README.md 文件。

/data/*
!/data/README.md

!/data/input/
/data/input/*
!/data/input/README.md

!/data/output/
/data/output/*
!/data/output/README.md

This is how I keep the structure of folders while ignoring everything else. You have to have a README.md file in each directory (or .gitkeep).

/data/*
!/data/README.md

!/data/input/
/data/input/*
!/data/input/README.md

!/data/output/
/data/output/*
!/data/output/README.md
孤云独去闲 2024-07-30 08:22:45

要从 .gitignore 中排除文件夹,可以执行以下操作。

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

这将忽略 bower_components 内的所有文件/子文件夹(/highcharts 除外)。

To exclude folder from .gitignore, the following can be done.

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

This will ignore all files/subfolders inside bower_components except for /highcharts.

不疑不惑不回忆 2024-07-30 08:22:45

关于此有很多类似的问题,所以我将发布我之前写的内容:

我让它在我的机器上工作的唯一方法是这样做:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
*.*

#Only Include these specific directories and subdirectories and files if you wish:
!wordpress/somefile.jpg
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*

注意你如何必须明确允许你的每个级别的内容想要包括在内。 因此,如果我在主题下有 5 个子目录,我仍然需要将其拼写出来。

这是来自 @Yarin 的评论:https://stackoverflow.com/a/5250314/1696153

这些都是有用的主题:

我也尝试过

*
*/*
**/**

**/wp-content/themes/**

/wp-content/themes/**/*

这些都不适合我。 很多线索和错误!

There are a bunch of similar questions about this, so I'll post what I wrote before:

The only way I got this to work on my machine was to do it this way:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
*.*

#Only Include these specific directories and subdirectories and files if you wish:
!wordpress/somefile.jpg
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*

Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories 5 deep under themes, I still need to spell that out.

This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153

These were useful topics:

I also tried

*
*/*
**/**

and **/wp-content/themes/**

or /wp-content/themes/**/*

None of that worked for me, either. Lots of trail and error!

清音悠歌 2024-07-30 08:22:45

我就是这样做的:

# Ignore everything
*

# Whitelist anything that's a directory
!*/

# Whitelist some files
!.gitignore

# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**

# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules

# Ignore this file recursively
**/.DS_Store

使用gig status -u递归地查看未跟踪目录中的单个文件 - 使用git status你只能看到文件夹,这可能会欺骗你认为他们里面的一切都被追踪

This is how I did it:

# Ignore everything
*

# Whitelist anything that's a directory
!*/

# Whitelist some files
!.gitignore

# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**

# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules

# Ignore this file recursively
**/.DS_Store

Use gig status -u to view individual files in untracked directories recursively - with git status you'd only see folders, which could fool you into thinking that everything inside them was tracked

梦萦几度 2024-07-30 08:22:45

我的子文件夹有问题。

不起作用:

/custom/*
!/custom/config/foo.yml.dist

起作用:

/custom/config/*
!/custom/config/foo.yml.dist

I had a problem with subfolder.

Does not work:

/custom/*
!/custom/config/foo.yml.dist

Works:

/custom/config/*
!/custom/config/foo.yml.dist
还如梦归 2024-07-30 08:22:45

不确定是否已经指出了这一点,但我在使用我希望 gitignore 忽略的文件夹前面的 ! 忽略被忽略文件夹内的子文件夹时遇到了麻烦。

但是我发现被忽略的文件夹的路径中没有 * 。 我的 .gitignore 看起来像这样:

/folder/
!/folder/subfolder

子文件夹仍然被忽略,但在文件夹后添加 * 使它像这样工作

/folder/*
!/folder/subfolder

Not sure if this has been pointed out already but I was having trouble ignoring a subfolder inside an ignored folder using the ! in front of the folder i want gitignore to ignore.

However I figured out that the folder that was being ignored had no * in its path. My .gitignore looked like this:

/folder/
!/folder/subfolder

The subfolder was still being ignored, but adding a * after folder made it work like so

/folder/*
!/folder/subfolder
红墙和绿瓦 2024-07-30 08:22:45

我尝试了上面给出的所有答案,但没有一个对我有用。 阅读 gitignore 文档(此处)后,我发现如果您首先排除某个文件夹,则文件名子文件夹中的内容未被索引。 因此,如果您随后使用感叹号来包含文件,则在索引中找不到该文件,因此不会包含在您的 git 客户端中。

这就是找到解决方案的方法。 我首先为文件夹树中的所有子文件夹添加例外以使其正常工作,这是一项艰巨的工作。 之后我能够将详细配置压缩到下面的配置,这与文档有点相反。

工作 .gitignore:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

结果我在我的 git 客户端中看到只有 Pro/3rdparty/domain/modulename 中的两个文件/ 文件夹正在为下一次提交而准备,这正是我正在寻找的。

如果您需要将同一文件夹的多个子文件夹列入白名单,则将排除语句下方的感叹号行分组,如下所示:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/

否则它将无法按预期工作。

I tried all answers as given here above, but none worked for me. After reading the gitignore documentation (here) i found out that if you exclude a folder first that the filenames in the subfolder are not being indexed. So if you use the exclamation mark afterwards to include a file, it is not found in the index and thus not being included in your git client.

That was the way to finding the solution. I started with adding exceptions for all subfolders in my folder tree to get it working, which is a hell of a job. Afterwards i was able to compact the detailed configuration to the configuration below, which is a bit contrary to the documentation..

Working .gitignore:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

As result i see in my git client that only the two files inside the Pro/3rdparty/domain/modulename/ folder are being staged for the next commit, and that was exactly what i was looking for.

And if you need to whitelist several subfolders of the same folder then group the exclamation mark lines below the exclude statement like this:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/

Else it wont work as expected.

糖果控 2024-07-30 08:22:45

这里有很多复杂的答案......
这是我在上面没有看到的一些非常简单的东西,并且在许多情况下都能很好地工作:

# ignore all files that have an extension
**/*.*

# except for these extensions
!**/*.extension1
!**/*.extension2

这不会忽略任何无扩展名文件,但是如果您有一堆具有相同或相似名称的文件,您可以为这些文件添加排除项

**/EXTENSIONLESSFILETOIGNORE

a lot of complex answers here...
here's something pretty simple that I don't see above and works well in many scenarios:

# ignore all files that have an extension
**/*.*

# except for these extensions
!**/*.extension1
!**/*.extension2

this doesn't ignore any extensionless files, but if you have a bunch that all have the same or similar name, you can add exclusions for those

**/EXTENSIONLESSFILETOIGNORE
滴情不沾 2024-07-30 08:22:45

这对我有用,我只想将一个 Cordova 插件提交到存储库:

...
plugins/*
!plugins/cordova-plugin-app-customization

That's what have worked for me, I wanted to commit only one Cordova plugin to the repo:

...
plugins/*
!plugins/cordova-plugin-app-customization
愚人国度 2024-07-30 08:22:45

我有来自 Bower 的 Jquery 和 Angular。 Bower 将它们安装在

/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files

最小化的 jquery 位于 dist 目录中,而 Angular 位于 angular 目录中。 我只需要最小化的文件即可提交到 github。 对 .gitignore 进行了一些篡改,这就是我设法想到的......

/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js

希望有人会发现这很有用。

I have Jquery and Angular from bower. Bower installed them in

/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files

The minimized jquery is inside the dist directory and angular is inside angular directory. I only needed minimized files to be commited to github. Some tampering with .gitignore and this is what I managed to conjure...

/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js

Hope someone could find this useful.

简单爱 2024-07-30 08:22:45

我解决此问题的最简单方法是强制添加文件。 即使它被埋藏或嵌套在 git 忽略的子目录树中,它也会在 git 中被考虑。

例如:

x64 文件夹被排除在 .gitignore 中:

x64/

但您想要包含位于 x64/Release/ 目录中的文件 myFile.py
然后你必须:

git add -f x64/Release/myFile.py

你可以对匹配模式的多个文件执行此操作,例如

git add -f x64/Release/myFile*.py

等等。

The simplest way that I go about this is to force add a file. It will be accounted for in git even if it is buried or nested inside a git-ignored subdirectory tree.

For example:

x64 folder is excluded in .gitignore:

x64/

But you want to include the file myFile.py located in x64/Release/ directory.
Then you have to:

git add -f x64/Release/myFile.py

You can do this for multiple files of files that match a pattern e.g.

git add -f x64/Release/myFile*.py

and so on.

ゝ杯具 2024-07-30 08:22:45

如果您需要忽略除少数文件和少数 root 文件夹之外的所有内容,则可以使用简单的解决方案:

/*
!.gitignore
!showMe.txt
!my_visible_dir

神奇之处在于 /* (如上所述),它会忽略(根)文件夹中的所有内容但不是递归的。

Simple solution if you need to ignore everything except few files and few root folders:

/*
!.gitignore
!showMe.txt
!my_visible_dir

The magic is in /* (as described above) it ignores everything in the (root) folder BUT NOT recursively.

天煞孤星 2024-07-30 08:22:45

我得到了这个工作

# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib

I got this working

# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib
迷鸟归林 2024-07-30 08:22:45

到目前为止,没有任何东西对我有用,因为我试图从库中添加一个罐子。

这不起作用:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 

这起作用了:

build/*
!build/libs

Nothing worked for me so far because I was trying to add one jar from lib.

This did not worked:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 

This worked:

build/*
!build/libs
波浪屿的海角声 2024-07-30 08:22:45

我在否定单个文件方面也遇到了一些问题。 我能够提交它们,但我的 IDE (IntelliJ) 总是抱怨被跟踪的忽略文件。

git ls-files -i --exclude-from .gitignore

显示了我以这种方式排除的两个文件:

public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

最后,这对我有用:

public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

关键是文件夹的否定 typo3conf/< /代码> 首先。

此外,似乎语句的顺序并不重要。 相反,您需要先显式否定所有子文件夹,然后才能否定其中的单个文件。

对于 .gitignore,文件夹 !public/typo3conf/ 和文件夹内容 public/typo3conf/* 是两个不同的东西。

很棒的线索! 这个问题困扰了我一段时间;)

I also had some issues with the negation of single files. I was able to commit them, but my IDE (IntelliJ) always complained about ignored files, which are tracked.

git ls-files -i --exclude-from .gitignore

Displayed the two files, which I've excluded this way:

public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

In the end, this worked for me:

public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

The key was the negation of the folder typo3conf/ first.

Also, it seems that the order of the statements doesn't matter. Instead, you need to explicitly negate all subfolders, before you can negate single files in it.

The folder !public/typo3conf/ and the folder contents public/typo3conf/* are two different things for .gitignore.

Great thread! This issue bothered me for a while ;)

乙白 2024-07-30 08:22:45

我似乎找到了一些对我有用但没有人提到的东西。

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
!subdir/
!subdir/**/*

基本上,它似乎否定了子目录被忽略,您必须有两个条目,一个用于子目录本身 !subdir/ ,然后另一个条目扩展到下的所有文件和文件夹它 !subdir/**/*

I seem to have found something that worked for me which no one else mentioned.

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
!subdir/
!subdir/**/*

Basically, it seems to negate a sub-directory from being ignored, you have to have two entries, one for the sub-directory itself !subdir/ and then another one which expands to all files and folders under it !subdir/**/*

人海汹涌 2024-07-30 08:22:45

这是一场灾难,在使用 git 16 年(2005 年)之后,没有简单明了的方法来排除位于深处的一个文件。目录层次结构,否则应被忽略。 相反,我们需要诉诸疯狂的事情,例如反复挖掘结构并以正确的顺序排除和包含目录。 一年要做四次的事情是绝对不可能记住的。

唯一聪明但非常奇怪的选择是使用 git add --force 。 当你不单独处理 gitted 存储库时,某些事情肯定会在某个时候失败。

所以我写了一个小的 bash 函数来为你解决这个问题,以便于复制粘贴。 让我首先解释一下这一行:

f=0; y='';for x in $(echo "uploads/rubbish/stuff/KEEP_ME/a.py" | tr '\/' '\n'); do y="$y/$x"; if [ $(($f)) -eq 0 ]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo;

# output:
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a.py

描述

  • 有一个if语句来调整第一个项目没有得到/
  • tr '\/' '\n' - 将标准 POSIX 路径中的 / 转换为 \n(以获取列表)
  • y="$y/$x" - 在上一个目录之后添加下一个子目录(在列表中)。
  • echo -e '!'"$y/\n$y/*" -
    打印 2 行子目录项,第一行使用 ! prefix,第二行使用 /* postfix
  • sed '$d' - 删除最后一行
  • sed -z 's/..$//' - 删除最后 2 个字符 /*最后一行

然后我们创建函数:

function gitkeep () { f=0; y='';for x in $(echo "$1" | tr '\/' '\n'); do y="$y/$x"; if [[ f -eq 0 ]]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo; }

# gitkeep "uploads/rubbish/stuff/KEEP_ME/a"

!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a

这里我将算术 if 语句简化为 if [[ f -eq 0 ]];

享受吧!

It's a disaster that after 16 years (2005) of using git, there is no simple and obvious way to exclude one file when located deep in a directory hierarchy, that should otherwise be ignored. Instead we need to resort to crazy things like repeatedly digging down the structure and excluding and including directories in the correct order. Something which is darn right impossible to remember those 4 times a year you need to do it.

The only smart, but very weird alternative, is to use the git add --force. Something which is bound to fail at some point, when you're not working on the gitted repo alone.

So I wrote a small bash function to fix this for you, for easy copy paste. Let me first explain the one liner:

f=0; y='';for x in $(echo "uploads/rubbish/stuff/KEEP_ME/a.py" | tr '\/' '\n'); do y="$y/$x"; if [ $(($f)) -eq 0 ]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo;

# output:
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a.py

Description

  • There is an if statement to adjust for the 1st item not getting a /.
  • tr '\/' '\n' - translate the / to \n in standard POSIX paths (to get a list)
  • y="$y/$x" - add the next sub-dirctory (in list) after the previous directory.
  • echo -e '!'"$y/\n$y/*" -
    print the 2 lines of sub-directory item, 1st with ! prefix, and 2nd with /* postfix.
  • sed '$d' - remove last line
  • sed -z 's/..$//' - remove last 2 characters /* from last line

Then we create the function:

function gitkeep () { f=0; y='';for x in $(echo "$1" | tr '\/' '\n'); do y="$y/$x"; if [[ f -eq 0 ]]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo; }

# gitkeep "uploads/rubbish/stuff/KEEP_ME/a"

!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a

Here I simplified the arithmetic if statement to if [[ f -eq 0 ]];.

Enjoy!

天气好吗我好吗 2024-07-30 08:22:45

上述片段效果很好,但有点令人困惑。 我通过失败和尝试找到了正确的方法。 这是我如何让它工作的;

  1. 我有一个名为 example_PostProcessingManager 的文件夹,
  2. 我想排除除 example_PostProcessingManager/src/ 文件夹内容之外的所有内容
  3. 另外,除了 example_PostProcessingManager/bin/data/ 文件夹内容

简而言之,我想保留 datasrc 文件夹的内容,并排除 example_PostProcessingManager 文件夹中的其他内容

为此,我编辑 <代码>.gitignore如下;

# Exclude everything inside the root folder as follows
example_PostProcessingManager/*

# Include only src folder
!example_PostProcessingManager/src

# Include also bin folder because the data folder is inside this folder. 
!example_PostProcessingManager/bin

# Exclude everything inside the bin folder. I know we excluded everything above already.
# It doesn't make sense but this is the way...
example_PostProcessingManager/bin/*

# Now include everything inside the data folder.
!example_PostProcessingManager/bin/data

PS主要思想是您需要从根文件夹开始并逐步访问目标文件夹。 如果您按如下方式执行此操作,它将仅包含 src 文件夹,并忽略 data 文件夹中的所有内容。

失败案例

example_PostProcessingManager/*
!example_PostProcessingManager/src
!example_PostProcessingManager/bin/data

The aforementioned snippets work well but they are a little bit confusing. I found the right way with fail and try. Here is how I manage it to work;

  1. I have a folder called example_PostProcessingManager
  2. I want to exclude everything except the example_PostProcessingManager/src/ folder contents
  3. Also except the example_PostProcessingManager/bin/data/ folder contents

Briefly, I want to keep the contents of data and src folders and exclude anything else inside the example_PostProcessingManager folder

To do that I edit the .gitignore as follows;

# Exclude everything inside the root folder as follows
example_PostProcessingManager/*

# Include only src folder
!example_PostProcessingManager/src

# Include also bin folder because the data folder is inside this folder. 
!example_PostProcessingManager/bin

# Exclude everything inside the bin folder. I know we excluded everything above already.
# It doesn't make sense but this is the way...
example_PostProcessingManager/bin/*

# Now include everything inside the data folder.
!example_PostProcessingManager/bin/data

P.S. the main idea is you need to start from root folder and access the destination folder step by step. If you do it as follows, it will only include the src folder and ignores everything inside the data folder.

FAILED CASE

example_PostProcessingManager/*
!example_PostProcessingManager/src
!example_PostProcessingManager/bin/data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文