取消排除在.gitignore中的文件

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

Revert excluded files from .gitignore

问题

我有以下的文件夹结构:

a/b/c/
a/b/d/s/g/foo.js
a/b/e/t/h/i/foo.js
a/b/f/u/j/foo.js

我试图忽略文件夹 a/b,但排除文件 foo.js

.gitignore 文件中,我尝试了以下规则:

a/b/*
!a/b/**/foo.js

但似乎不起作用。

有什么建议吗?

英文:

I have the following folder structure:

a/b/c/
a/b/d/s/g/foo.js
a/b/e/t/h/i/foo.js
a/b/f/u/j/foo.js

and I am trying to ignore the folder a/b but exclude the file foo.js

In .gitignore I have tried

a/b/*
!a/b/**/foo.js

but it doesn't seem to work.

Any suggestions?

答案1

得分: 1

问题在于 a/b/* 匹配目录 a/b/ca/b/d 等等,因此取消忽略那些已经被忽略的目录内的文件是无效的。

我认为你可以首先忽略所有路径,然后取消忽略所有目录(末尾的斜杠 - Git 不跟踪空目录),最后才能取消忽略你想要的特定文件:

a/b/**/*
!a/b/**/*/
!a/b/**/foo.js
英文:

The issue is that a/b/* matches the directories a/b/c, a/b/d, etc., so un-ignoring files inside those already-ignored directories doesn’t help.

I think you can start by ignoring all paths, then un-ignoring all directories (the trailing slash – Git doesn’t track empty directories), to be able to finally un-ignore the specific files you want:

a/b/**/*
!a/b/**/*/
!a/b/**/foo.js

答案2

得分: 0

首先,使用以下命令检查您的 Git 版本:

git --version

以确定哪个选项适合您。

根据 gitignore 文档,对于版本低于 2.41 和高于 2.34 的情况,类似以下结构应该适用:

a/*
!a/b.py

因此,以下结构应该适用:

a/b/**
!a/b/**/
!a/b/**/foo.js

请确保将此结构放在位于 Git 存储库根目录的 .gitignore 文件中。

英文:

First, check your Git version using:

git --version

to see which option is best for you.

According to the gitignore documentation for versions lower than 2.41 and higher than 2.34, a structure similar to the following should work:

a/*
!a/b.py

Therefore, a structure like the following should work:

a/b/**
!a/b/**/
!a/b/**/foo.js

Make sure to place this structure in your .gitignore file, located at the root of your Git repository.

huangapple
  • 本文由 发表于 2023年6月2日 03:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385107.html
匿名

发表评论

匿名网友

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

确定