如何在不使用 .gitignore 的情况下让 Git 忽略文件? [英] How do you make Git ignore files without using .gitignore?
问题描述
由于外部奇怪的限制,我无法修改存储库的 .gitignore
.除了修改 .gitignore
之外,有没有办法忽略文件和目录?即使它是一个全局解决方案,例如将应用于我所有存储库的全局配置.
Due to external weird constraints I cannot modify the .gitignore
of my repository. Is there a way to ignore files and directories other than modifying a .gitignore
? Even if it is a global solution like a global configuration that will be applied to all my repositories.
推荐答案
不要忘记,根据 gitignore,在 Git 考虑的不同忽略模式源"中存在优先顺序:
Do not forget, according to gitignore, that there is an order of precedence in the different "ignore pattern sources" that Git consider:
- 从命令行读取支持它们的命令的模式.
- 从与路径相同的目录或任何父目录中的 .gitignore 文件中读取的模式,高级文件中的模式(直到根目录)被低级文件中的模式覆盖,直到包含文件.
- 从
$GIT_DIR/info/exclude
读取的模式. - 从配置变量
core.excludesfile
指定的文件中读取的模式.
- Patterns read from the command line for those commands that support them.
- Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the root) being overridden by those in lower level files down to the directory containing the file.
- Patterns read from
$GIT_DIR/info/exclude
. - Patterns read from the file specified by the configuration variable
core.excludesfile
.
最后两个可以解决您的问题,但是:
The last two can be a solution for your problem but:
- 不会为远程存储库复制它们
- 他们可以让其他来源覆盖他们的模式
(另见此 SO 问题)
另外两个解决方案涉及更新索引(git update-index代码>
):
The other two solutions involve updating the index (git update-index
):
git update-index --assume-unchanged
:请参阅Git:在本地存储库中取消跟踪文件并将其保存在远程仓库中".
Elijah Lynn 在 评论.- 您甚至可以忽略文件夹内容:"
git update-index --assume-unchanged
在目录上". - 使用
--no-assume-unchange
来反转效果:参见Is it possible togit add
当前由assume-unchanged
? 保护的文件.
git update-index --assume-unchanged
: see "Git: untrack a file in local repo only and keep it in the remote repo".
It is mentioned by Elijah Lynn in the comments.- You can even ignore a folder content: "
git update-index --assume-unchanged
on directory". - Use
--no-assume-unchange
to reverse the effect: See "Is it possible togit add
a file currently protected byassume-unchanged
?".
但是,当您检出另一个分支或
git pull
时,忽略"状态可能会被重置.因此另一个选择:However, when you checkout another branch or when you
git pull
, that "ignore" status might be reset. Hence the other option:git update-index --skip-worktree
;看:git update-index --skip-worktree
; see:- "ignore my changes in files but don't delete them from remote rep" and
- "Preserve git --assume-unchanged files between branch checkouts".
两者之间的区别在Git - '
assume-unchanged
' 和 ' 之间的区别skip-worktree
'".The difference between the two is explained in "Git - Difference Between '
assume-unchanged
' and 'skip-worktree
'".这篇关于如何在不使用 .gitignore 的情况下让 Git 忽略文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- You can even ignore a folder content: "
- 您甚至可以忽略文件夹内容:"