Git排除分支文件 [英] Git excludesfile for a branch
问题描述
我想忽略分支中的某些文件,而不必依赖于跟踪的 .gitignore
文件,这个文件将在与其他分支合并期间被覆盖。
$ b
我一直遵循 Stack Overflow answer 和链接的博客帖子,但我的回购似乎没有认出指定的在我的
。 Git文档( .git / config
中排除文件 git-config
, gitignore code>
)似乎没有显示如何为特定分支指定和解决排除文件。
我的 .git / config
看起来像这样:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
excludesfile = + / info / exclude
[branchmyspecialbranch]
excludesfile = + / info / exclude_specialbranch
我的 .git / info / exclude
文件看起来像这样(默认情况下,我没有碰它):
#git ls-files --other --exclude-from = .git / info / exclude
#以'#'开头的行是注释。
#对于大多数情况下以C语言编写的项目,以下是一组很好的
#排除模式(如果要使用它们,请取消注释):
#*。[oa]
$ *
.DS_Store
我没有 .gitignore
在我的specialbranch文件中。
如果我试图忽略像 info.php这样的文件
,我的 .git / info / exclude_specialbranch
文件如下所示:
info.php
...但它不会忽略该文件。
如果我运行 git status --ignored
,它只会列出 .DS_Store
文件从默认排除
文件。
然而,如果我添加 info.php
到我的 .git / info / exclude
文件:
#git ls-files --other --exclude-from = .git / info / exclude
#以'#'开头的行是注释。
#对于大多数情况下以C语言编写的项目,以下是一组很好的
#排除模式(如果要使用它们,请取消注释):
#*。[oa]
#*〜
.DS_Store
info.php
它会忽略文件完美。
请注意,它仍然可以在<$ c>下排除 excludesfile = + / info / exclude
$ c> [core] 放入 .git / config
中。 Git似乎知道系统范围内的 exclude
不需要告诉它。
我有一种感觉 .git / config
不会识别我的自定义排除文件的地址:
excludesfile = + / info / exclude_specialbranch
...很可能是因为使用了 +
来描述 .git
目录的位置。
在Git的(最近的)版本中处理自定义排除文件的正确方法是什么?
我正在运行OSX 10.9.5和Git 2.2.1版本。
Git不支持每分支排除文件
重新尝试实现一些Git不支持的功能。 博客文章是这个堆栈溢出答案的恶作剧只是parroted。正如在该答案下的评论中指出的那样,即使是原始的博客帖子也包含讨论,表明解决方案不起作用,并且链接到更新的博客文章,提到即使作者也无法重现该行为。
为什么它不起作用?如果您阅读 man gitignore
和< a href =http://git-scm.com/docs/git-config =nofollow noreferrer> man git-config
,你会发现只是 core.excludesfile
引用。没有分支。< name> .excludesfile
那里。 core.excludesfile
旨在让您排除例如Vim .swp文件或其他软件使用的临时文件。
core.excludesfile
除了.gitignore
(每个目录)和.git / info /排除
,Git会在这个文件中查找那些不需要跟踪的文件模式。〜/
扩展为$ HOME
和<〜user /
到指定用户的主目录。它的默认值是$ XDG_CONFIG_HOME / git / ignore
。如果$ XDG_CONFIG_HOME
未设置或为空,则使用$ HOME / .config / git / ignore
代替。请参阅 gitignore(5)。
解决方法
我相信每个分支的最佳逼近排除文件是通过使用 post-checkout hook 并通过 .gitignore
实现一个符号链接。
每个分支都有例如一个 .gitignores
目录,其文件以相应的分支命名。然后会有一个默认使用的 .gitignores / __ default
文件。 .gitignore
将被所有排除文件排除,并且将由post-checkout钩子创建,作为符号链接到 .gitignores中的相应文件
。
如果您不想跟踪排除文件,您可以使用 .git / info / exclude
文件作为符号链接到 .git / info / excludes / __ default
等。
I want to ignore certain files within a branch without having to rely on a tracked .gitignore
file that will be overwritten during merges with other branches.
I’ve closely followed a Stack Overflow answer along with the linked blog post, but my repo doesn’t seem to be recognizing the specified excludesfile
in my .git/config
. The Git documentation (git-config
, gitignore
) doesn’t seem to show how to specify and address an excludes file for a specific branch.
My .git/config
looks like this:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
excludesfile = +/info/exclude
[branch "myspecialbranch"]
excludesfile = +/info/exclude_specialbranch
My .git/info/exclude
file looks like this (by default, I didn’t touch it):
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
I don’t have a .gitignore
file in my "specialbranch".
If I try to ignore a file like info.php
, my .git/info/exclude_specialbranch
file looks like this:
info.php
… but it doesn’t ignore the file.
If I run git status --ignored
, it only lists the .DS_Store
file from the default exclude
file.
However, if I add info.php
to my .git/info/exclude
file:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
info.php
it ignores the file perfectly.
Note that it still works without the line excludesfile = +/info/exclude
under [core]
in .git/config
. Git seems to know where the system-wide exclude
is without me having to tell it.
I have a feeling that .git/config
isn’t recognizing the address of my custom excludes file:
excludesfile = +/info/exclude_specialbranch
… most likely because of the use of +
to describe the location of the .git
directory.
What’s the correct method for addressing custom exclude files in the (more recent) versions of Git?
I’m running OSX 10.9.5 and Git version 2.2.1.
Git does not support per-branch excludes files
You’re trying to achieve something that Git does not support. The blog post is the original source of this hoax that the Stack Overflow answer only parroted. As noted in comments under that answer, even the original blog post contains discussion that brings out that the solution does not work and it links to a newer blog post that mentions that even the author is unable to reproduce the behavior.
Why it does not work? If you read man gitignore
and man git-config
, you’ll find just core.excludesfile
referenced. No branch.<name>.excludesfile
there. The core.excludesfile
is meant to enable you to exclude e.g. Vim .swp files or other temporary stuff your software uses.
core.excludesfile
In addition to
.gitignore
(per-directory) and.git/info/exclude
, Git looks into this file for patterns of files which are not meant to be tracked. "~/
" is expanded to the value of$HOME
and "~user/
" to the specified user’s home directory. Its default value is$XDG_CONFIG_HOME/git/ignore
. If$XDG_CONFIG_HOME
is either not set or empty,$HOME/.config/git/ignore
is used instead. See gitignore(5).
Work-around
I believe that the best approximation of a per-branch excludes file is achieved using the post-checkout hook and realization of the .gitignore
via a symlink.
Each branch would have e.g. a .gitignores
directory with files named after the corresponding branches. Then there would be a .gitignores/__default
file that would be used by default. The .gitignore
would be excluded by all the excludes files and would be created by the post-checkout hook as a symlink to the corresponding file in .gitignores
.
If you don’t want to track the excludes files, you may do the same with .git/info/exclude
file as a symlink to .git/info/excludes/__default
etc.
这篇关于Git排除分支文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!