git 忽略异常

发布于 2024-09-08 15:03:23 字数 117 浏览 8 评论 0原文

我有一个 gitignore 文件,它使 git 忽略 *.dll 文件,这实际上是我想要的行为。但是,如果我想要一个异常(即能够提交foo.dll),我该如何实现呢?

I have a gitignore file that makes git ignore *.dll files, and that is actually the behavior I want. However, if I want an exception ( i.e. to be able to commit foo.dll), how can I achieve this?

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

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

发布评论

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

评论(12

葬﹪忆之殇 2024-09-15 15:03:23

使用:

*.dll    #Exclude all dlls
!foo.dll #Except for foo.dll

来自 gitignore

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

Use:

*.dll    #Exclude all dlls
!foo.dll #Except for foo.dll

From gitignore:

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.

£噩梦荏苒 2024-09-15 15:03:23

如果你写: Git 会忽略文件夹,

/js

但如果你这样做,它就不能添加例外:
!/js/jquery!/js/jquery/!/js/jquery/*

你必须这样写:

/js/* 

只有这样你才能除了这样的子文件夹

!/js/jquery

Git ignores folders if you write:

/js

but it can't add exceptions if you do:
!/js/jquery or !/js/jquery/ or !/js/jquery/*

You must write:

/js/* 

and only then you can except subfolders like this

!/js/jquery
万水千山粽是情ミ 2024-09-15 15:03:23

您只需git add -f path/to/foo.dll即可。

.gitignore 仅忽略用于常规跟踪的文件和诸如 git add 之类的内容。

You can simply git add -f path/to/foo.dll.

.gitignore ignores only files for usual tracking and stuff like git add .

抠脚大汉 2024-09-15 15:03:23

要排除目录中的所有内容,但某些子目录,请执行以下操作:

wp-content/*
!wp-content/plugins/
!wp-content/themes/

来源:https://gist.github.com/444295

To exclude everything in a directory, but some sub-directories, do the following:

wp-content/*
!wp-content/plugins/
!wp-content/themes/

Source: https://gist.github.com/444295

沦落红尘 2024-09-15 15:03:23

只需在排除规则前添加 ! 即可。

根据 gitignore 手册页

模式具有以下格式:

...

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

Just add ! before an exclusion rule.

According to the gitignore man page:

Patterns have the following format:

...

  • 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.
百变从容 2024-09-15 15:03:23

对于嵌套文件夹,我根据Matiss的答案提出了一个解决方案。

假设我想忽略 build/ 目录(位于 /app 中)中的所有内容。所以我这样做:

build/*

但是,如果我想排除 build/outputs/bundle/release 子文件夹,我需要玩一些捉迷藏!


/app/build/*
!/app/build/outputs
/app/build/outputs/*
!/app/build/outputs/bundle
/app/build/outputs/bundle/*
!/app/build/outputs/bundle/release

重要提示:

  • 所有路径都应以 / 开头,并且相对于 .gitignore
  • 您必须一次只处理一个子文件夹。您可以在 VS Code 中(例如)查看每一步包含的内容和不包含的内容。

For Nested Folders, I came up with a solution based on Matiss's answer.

Let's say I want to ignore everything in build/ directory (which is in /app). So I do:

build/*

However, if I want to exclude build/outputs/bundle/release subfolder, I need to play some hide and seek!


/app/build/*
!/app/build/outputs
/app/build/outputs/*
!/app/build/outputs/bundle
/app/build/outputs/bundle/*
!/app/build/outputs/bundle/release

Important Notes:

  • All the paths should start with / and be relative to the .gitignore
  • You have to do it one subfolder at a time. You can see in VS Code (for instance) what it includes and what not at every step.
梦里梦着梦中梦 2024-09-15 15:03:23

.gitignore 中的 !foo.dll ,或(每次!)git add -f foo.dll

!foo.dll in .gitignore, or (every time!) git add -f foo.dll

执笔绘流年 2024-09-15 15:03:23

如果您使用 Visual Studio 并且您的 .dll 恰好位于 bin 文件夹中,那么您需要为特定 bin 文件夹本身添加例外,然后才能添加以下例外: .dll 文件。例如,

!SourceCode/Solution/Project/bin
!SourceCode/Solution/Project/bin/My.dll

这是因为默认的 Visual Studio .gitignore 文件包含 [Bbin]/ 的忽略模式,

此模式会切换所有 bin 文件夹(及其内容),这使得任何包含内容的尝试都是多余的(因为文件夹本身已被忽略)。

我能够找到为什么我的文件没有通过

git check-ignore -v -- SourceCode/Solution/Project/bin/My.dll

从 Git Bash 窗口运行而被排除。这返回了 [Bbin]/ 模式。

If you're working with Visual Studio and your .dll happens to be in a bin folder, then you'll need to add an exception for the particular bin folder itself, before you can add the exception for the .dll file. E.g.

!SourceCode/Solution/Project/bin
!SourceCode/Solution/Project/bin/My.dll

This is because the default Visual Studio .gitignore file includes an ignore pattern for [Bbin]/

This pattern is zapping all bin folders (and consequently their contents), which makes any attempt to include the contents redundant (since the folder itself is already ignored).

I was able to find why my file wasn't being excepted by running

git check-ignore -v -- SourceCode/Solution/Project/bin/My.dll

from a Git Bash window. This returned the [Bbin]/ pattern.

神爱温柔 2024-09-15 15:03:23

解决方案取决于 gitignore 规则和异常规则之间的关系:

  1. 文件/同级文件:使用 @Skilldrick 解决方案
  2. 文件夹/子文件夹:使用 @Matiss Jurgelis 解决方案
  3. 不同级别的文件/文件或文件/子文件夹:您可以这样做:

    <前><代码>*.suo
    *。用户
    *.userosscache
    *.sln.docstates

    # ...

    # 整个子文件夹的例外
    !SetupFiles/elasticsearch-5.0.0/**/*
    !SetupFiles/filebeat-5.0.0-windows-x86_64/**/*

    # 不同级别文件的例外情况
    !SetupFiles/kibana-5.0.0-windows-x86/**/*.suo
    !SetupFiles/logstash-5.0.0/**/*.suo

The solution depends on the relation between the git ignore rule and the exception rule:

  1. Files/Files at the same level: use the @Skilldrick solution.
  2. Folders/Subfolders: use the @Matiss Jurgelis solution.
  3. Files/Files in different levels or Files/Subfolders: you can do this:

    *.suo
    *.user
    *.userosscache
    *.sln.docstates
    
    # ...
    
    # Exceptions for entire subfolders
    !SetupFiles/elasticsearch-5.0.0/**/*
    !SetupFiles/filebeat-5.0.0-windows-x86_64/**/*
    
    # Exceptions for files in different levels
    !SetupFiles/kibana-5.0.0-windows-x86/**/*.suo
    !SetupFiles/logstash-5.0.0/**/*.suo
    
倾城花音 2024-09-15 15:03:23

这就是我的做法,每个目录中都有一个 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 do it, with a README.md file in each directory:

/data/*
!/data/README.md

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

!/data/output/
/data/output/*
!/data/output/README.md
水中月 2024-09-15 15:03:23

如果您有一个目录并且想要忽略除某些文件(例如 *.py 文件)之外的所有内容,您可以执行以下操作:

sub-dir/**/*.*
!sub-dir/**/*.py

If you have a directory and want to ignore everything with the exception of some files (e.g. *.py files), you can do:

sub-dir/**/*.*
!sub-dir/**/*.py
零時差 2024-09-15 15:03:23

从 Git 2.7.0 开始,Git 将考虑异常。来自官方发行说明:

  • 允许后面的“!/abc/def”覆盖前面的“/abc”
    出现在同一个.gitignore文件中,更容易表达
    “/abc 目录中的所有内容都将被忽略,除了......”。

https://raw.githubusercontent.com/git/ git/master/Documentation/RelNotes/2.7.0.txt

编辑:显然,自 Git 2.8.0 以来,这不再起作用

Since Git 2.7.0 Git will take exceptions into account. From the official release notes:

  • Allow a later "!/abc/def" to override an earlier "/abc" that
    appears in the same .gitignore file to make it easier to express
    "everything in /abc directory is ignored, except for ...".

https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.7.0.txt

edit: apparently this doesn't work any more since Git 2.8.0

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