解决方案A:
1. 前置条件:在向.gitignore添加忽略文件之前,一定要保证那些文件未被git跟踪,
因此,可能需要先使用命令
git rm --cached filename
将它们从索引中删除。
2. 本地仓库忽略
本地仓库的文件忽略规则可以在 .git/info/exclude 文件中添加。这些忽略的文件不会提交到共享库中,因而不会被协作者所共享。
exclude文件:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
ignored_a_file.txt
ignored_a_file.txt 为要被忽略的文件。
3. 在本仓库目录下,运行:
git ls-files --others --exclude-from=.git/info/exclude
注: 在本仓库目录下,运行上条命令行。
解决方案B:
1. 前置条件:同解决方案A。
2. 当前工作目录添加文件忽略
对于每一级工作目录,创建一个.gitignore文件,向该文件中添加要忽略的文件或目录。
exclude文件,文件内容的格式,每一行为一个需要被忽略的文件名,含扩展名。
但在创建并编辑这个文件之前,一定要保证要忽略的文件没有添加到git索引中。
如需要,使用命令git rm --cached filename将要忽略的文件从索引中删除。
3. 在本仓库目录下,或其他能满足该条指令运行的目录下。运行:
git config --global core.excludesfile ~/.gitignore
注: 这是操作系统的目录路径。
注意C:
1. 如果步骤1~3后,还是存在无法忽略时,随意修改源程序,以保证能够再次运行 build,这样就OK了。
2. .gitignore 和.git/info/exclude的主要区别,是最后的第3步运行的命令,不同。
3. 似乎这样运行,也可以避免 commit提交:git restore <file> # <file>包含系统路径
$ git status --ignored
HEAD detached at v0.0.1
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .gitignore
modified: ignored_a_file.txt
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
ignored_b_file.txt
no changes added to commit (use "git add" and/or "git commit -a")
摘抄D: .gitignore的格式规范
• 所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
• 可以使用标准的 glob 模式匹配。
• 匹配模式最后跟反斜杠(/)说明要忽略的是目录。
• 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。