Git忽略特定路径中的文件夹。

huangapple go评论58阅读模式
英文:

Git Ignore folders in specific paths

问题

我在gitignore文件中有一个规则,用于排除文件夹名为"bar"的文件夹,但如果"bar"文件夹位于"foo"下面,我想要"取消忽略"。

  • 忽略:
    bar/

  • 但允许:
    foo/bar

是否可能?我该如何做?

我尝试过:

bar/
!foo/bar/

以及

bar/
foo/*
!foo/bar/

以及许多其他选项,但都没有成功。

发生的情况是它要么忽略(阻止提交)所有"bar"文件夹,要么一个都不忽略,我无法使其仅忽略位于"foo"之外的一个。

英文:

I have a rule on gitignore file to exclude folders with name "bar" but I want to "un-ignore" if the "bar" folder is below "foo"

  • ignore:
    bar/

  • but allow:
    foo/bar

Is it possible? How can I do it?

I tryed:

bar/
!foo/bar/

and

bar/
foo/*
!foo/bar/

and many other options but no luck.

what happens is that it ignores (prevent commit) both "bar" folders or none, I can't make it ignore only the one that is outside "foo"

答案1

得分: 1

第一个解决方案您提到的方法有效。

执行 git status --untracked-files=all . 命令时没有 .gitignore 文件的输出如下:

未跟踪的文件:
  (使用 "git add <file>..." 命令将其包含到提交中)
        bar/something.txt
        foo/bar/something.txt

因此,我们看到有两个文件是新的并且被报告为 "未跟踪"。

如果我们现在创建一个 .gitignore 文件,并包含以下内容:

bar/
!foo/bar/

输出会改变为:

未跟踪的文件:
  (使用 "git add <file>..." 命令将其包含到提交中)
        .gitignore
        foo/bar/something.txt

因此,git

  • 检测到我们新创建的 .gitignore 文件
  • 忽略了仍然存在于文件系统中的 bar/something.txt
  • 但不忽略了 foo/bar/something.txt
英文:

> Is it possible? How can I do it?

The first solution you mentioned works.

Output of git status --untracked-files=all . without the .gitignore file:

Untracked files:
  (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)
        bar/something.txt
        foo/bar/something.txt

So we see two files are new and reported as "untracked".

If we now create a .gitignore file with the following content

bar/
!foo/bar/

the output changes to

Untracked files:
  (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)
        .gitignore
        foo/bar/something.txt

so git

  • picks up our newly created .gitignore file
  • ignores the bar/something.txt which is still present on the filesystem
  • but does not ignore the foo/bar/something.txt

huangapple
  • 本文由 发表于 2023年6月22日 19:42:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定