如何添加被 git 忽略规则忽略的文件

How to add files ignored by a git ignore rule

在我的 .gitignore 中,我有:

*.framework
*.a

但是,我在各种项目中使用这些符号链接,并且由于符号链接和 .a.framework,我似乎无法将它们签入 Git。

是否有我遗漏的命令或变通办法?我想签入符号链接,这样我就不必每次都创建它们。如果需要,我总是可以写一个 bash 脚本来制作这些。

Git 仅使用路径来确定它是否应该忽略文件 - 因此,如果有可能派生出一种模式,该模式仅捕获您不需要的文件并排除您想要的所有文件确实想要 - 这就是要走的路。

但是,请记住 git 忽略规则并不是一成不变的; 总是 可以添加文件,您只需要使用 --force 标志:

$ cat .gitignore 
*.a
$ git status --ignored
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
# Ignored files:
#   (use "git add -f <file>..." to include in what will be committed)
#
#   bar.a
#   foo.a
nothing added to commit but untracked files present (use "git add" to track)
$ git add foo.a
The following paths are ignored by one of your .gitignore files:
foo.a
Use -f if you really want to add them.
fatal: no files added
$

在这种情况下,似乎经常被遗漏的是帮助输出中的这一行:

Use -f if you really want to add them.

利用强制选项,添加被忽略的文件:

$ git add -f foo.a
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   foo.a
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
$  git status --ignored
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   foo.a
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
# Ignored files:
#   (use "git add -f <file>..." to include in what will be committed)
#
#   bar.a
$