问题描述
我希望有一个git仓库只跟踪名为eg的文件 SOURCES ,而其他所有内容都应该被忽略(例如,每个 SOURCES 文件列出其起源的pdf文件树)。最简单的拍摄应该是
*
!SOURCES
位于 .gitignore 中。但是排除例如 A / SOURCES 被 * 所覆盖,要求我使用 git add -f 。如何修改 .gitignore 以忽略除< SOURCES 之外的所有文件,而无需强制添加?
编辑从发布的解决方案不会目录结构不固定,即不应该将包含 SOURCES 文件的新目录添加到 .gitignore 中手... ...
使用 .gitignore
Git不会跟踪路径。它只跟踪对象(〜文件)。
那么,为什么不反转表格:
git add -f - * / * / SOURCES * / SOURCES
或
shopt -s globstar
git add -f - ** / SOURCES
或者拿出大枪:
git add -f - $(find -type f -name SOURCES)
甚至是
$ p $ find -type f -name SOURCES -exec git add -f - {} \ +
未经测试的想法也许像这样的事情可能在pre-commit钩子中?
更新更多自动化的想法:
添加到.git / config中。
[别名]
ac =!_(){git add -f - * / * / SOURCES&&git commit \$ @ \;}; _
现在,您可以说
git commit -m像你通常工作'
,它会自动添加 * / * /来源
I'd like to have a git repository track only files named e.g. SOURCES while everything else shall be ignored (take e.g. a tree of pdf files where each SOURCES file lists their origins). The simplest shot would have been
* !SOURCES
in .gitignore. However the exclusion of e.g. A/SOURCES is overridden by the *, requiring me to use git add -f. How can .gitignore be modified to ignore everything except files named SOURCES without requiring a forced add?
edit The solution posted here will not do since the directory structure is not fixed, i.e. new directories containing a SOURCES file should not have to be added to .gitignore by hand...
You can't achieve this using just .gitignore
Git doesn't track paths. It tracks objects (~ files) only.
So, why don't you reverse the tables:
git add -f -- */*/SOURCES */SOURCES
or
shopt -s globstar git add -f -- **/SOURCES
Or get out the big guns:
git add -f -- $(find -type f -name SOURCES)
or even
find -type f -name SOURCES -exec git add -f -- {} \+
Untested idea Perhaps something like this could be in a pre-commit hook?
Update An idea for more automation:
Add this to .git/config
[alias] ac = "!_() { git add -f -- */*/SOURCES && git commit \"$@\"; }; _"
Now, you can just say
git commit -m 'like you usually work'
and it will automatically add the */*/SOURCES
这篇关于如何让gitignore跟踪文件的子目录实例而忽略其他所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!