git 忽略异常
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用:
来自 gitignore:
Use:
From gitignore:
如果你写: Git 会忽略文件夹,
但如果你这样做,它就不能添加例外:
!/js/jquery
或!/js/jquery/
或!/js/jquery/*
你必须这样写:
只有这样你才能除了这样的子文件夹
Git ignores folders if you write:
but it can't add exceptions if you do:
!/js/jquery
or!/js/jquery/
or!/js/jquery/*
You must write:
and only then you can except subfolders like this
您只需
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 likegit add .
要排除目录中的所有内容,但某些子目录,请执行以下操作:
来源:https://gist.github.com/444295
To exclude everything in a directory, but some sub-directories, do the following:
Source: https://gist.github.com/444295
只需在排除规则前添加
!
即可。根据 gitignore 手册页:
Just add
!
before an exclusion rule.According to the gitignore man page:
对于嵌套文件夹,我根据Matiss的答案提出了一个解决方案。
假设我想忽略
build/
目录(位于/app
中)中的所有内容。所以我这样做:但是,如果我想排除
build/outputs/bundle/release
子文件夹,我需要玩一些捉迷藏!重要提示:
/
开头,并且相对于.gitignore
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:However, if I want to exclude
build/outputs/bundle/release
subfolder, I need to play some hide and seek!Important Notes:
/
and be relative to the.gitignore
.gitignore 中的
!foo.dll
,或(每次!)git add -f foo.dll
!foo.dll
in .gitignore, or (every time!)git add -f foo.dll
如果您使用 Visual Studio 并且您的 .dll 恰好位于
bin
文件夹中,那么您需要为特定 bin 文件夹本身添加例外,然后才能添加以下例外: .dll 文件。例如,这是因为默认的 Visual Studio
.gitignore
文件包含[Bbin]/
的忽略模式,此模式会切换所有 bin 文件夹(及其内容),这使得任何包含内容的尝试都是多余的(因为文件夹本身已被忽略)。
我能够找到为什么我的文件没有通过
从 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.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
from a Git Bash window. This returned the
[Bbin]/
pattern.解决方案取决于 gitignore 规则和异常规则之间的关系:
不同级别的文件/文件或文件/子文件夹:您可以这样做:
<前><代码>*.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:
Files/Files in different levels or Files/Subfolders: you can do this:
这就是我的做法,每个目录中都有一个 README.md 文件:
This is how I do it, with a README.md file in each directory:
如果您有一个目录并且想要忽略除某些文件(例如
*.py
文件)之外的所有内容,您可以执行以下操作:If you have a directory and want to ignore everything with the exception of some files (e.g.
*.py
files), you can do:从 Git 2.7.0 开始,Git 将考虑异常。来自官方发行说明:
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:
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