git 尽管存在 .gitignore 文件,但仍强制添加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8006393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:14:22  来源:igfitidea点击:

Force add despite the .gitignore file

gitgitignore

提问by Mark

Is there a way to force gitto add a file despite the .gitignorefile?

git尽管有.gitignore文件,有没有办法强制添加文件?

回答by Daniel B?hmer

See man git-add:

man git-add

   -f, --force
       Allow adding otherwise ignored files.

So run this

所以运行这个

git add --force my/ignore/file.foo

回答by A-S

Despite Daniel B?hmer's working solution, Ohad Schneider offered a better solution in a comment:

尽管 Daniel B?hmer 提供了可行的解决方案,但 Ohad Schneider 在评论中提供了更好的解决方案:

If the file is usually ignored, and you force adding it - it can be accidentally ignored again in the future (like when the file is deleted, then a commit is made and the file is re-created.

如果该文件通常被忽略,并且您强制添加它 - 将来可能会意外地再次忽略它(例如当文件被删除时,然后进行提交并重新创建文件。

You should just un-ignore it in the .gitignore file like that: Unignore subdirectories of ignored directories in Git

您应该在 .gitignore 文件中取消忽略它,如下所示: Unignore subdirectories of denied directory of Git in Git

回答by shardy

Another way of achieving it would be to temporary edit the gitignore file, add the file and then revert back the gitignore. A bit hacky i feel

实现它的另一种方法是临时编辑 gitignore 文件,添加文件,然后恢复 gitignore。我觉得有点 hacky