git 忽略异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3203228/
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 08:41:42  来源:igfitidea点击:

git ignore exception

gitgitignore

提问by roundcrisis

I have a gitignore file that makes git ignore *.dllfiles, 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?

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

回答by Skilldrick

Use:

用:

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

From gitignore:

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.

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

回答by Matiss

Git ignores folders if you write:

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

/js

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

但如果你不能添加例外: !/js/jquery!/js/jquery/!/js/jquery/*

You must write:

你必须写:

/js/* 

and only then you can except subfolders like this

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

!/js/jquery

回答by Nils Riedemann

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

你可以简单地git add -f path/to/foo.dll

.gitignoreignores only files for usual tracking and stuff like git add .

.gitignore只忽略通常跟踪的文件和类似的东西 git add .

回答by Simon Paarlberg

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

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

回答by Robert Munteanu

Just add !before an exclusion rule.

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

According to the gitignore man page:

根据gitignore 手册页

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.

模式具有以下格式:

...

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

回答by Tobias Kienzler

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

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

回答by DeanOC

If you're working with Visual Studio and your .dll happens to be in a binfolder, 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.

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

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

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

这是因为默认的 Visual Studio.gitignore文件包含一个忽略模式[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).

这种模式正在清除所有 bin 文件夹(以及它们的内容),这使得任何包含内容的尝试都是多余的(因为文件夹本身已经被忽略)。

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.

从 Git Bash 窗口。这返回了[Bbin]/模式。

回答by Rosberg Linhares

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

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

  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
    
  1. 同一级别的文件/文件:使用@Skilldrick 解决方案
  2. 文件夹/子文件夹:使用@Matiss Jurgelis 解决方案
  3. 不同级别的文件/文件或文件/子文件夹:您可以这样做:

    *.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
    

回答by Erfan

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

由于 Git 2.7.0 Git 会考虑例外情况。来自官方发行说明:

  • 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 ...".
  • 允许稍后的“!/abc/def”覆盖出现在同一个 .gitignore 文件中的较早的“/abc”,以便更容易地表达“/abc 目录中的所有内容都被忽略,除了……”。

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

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