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

3

在我的.gitignore文件中,我有:

*.framework
*.a

然而,我在多个项目中使用符号链接,并且由于符号链接位于 .a 或者 .framework 中,我似乎无法将它们检入Git。

我是否遗漏了某个命令或者有什么变通方法? 我想检查这些符号链接以便不必每次都创建它们。 如果需要,我可以编写一个Bash脚本来制作这些符号链接。


1
请注意,.framework被视为目录,因此这可能只需要使用行*.framework/(注意尾随/)即可解决。如前所述,您可能需要rm --cached之前提交的框架。 - brichins
1个回答

3

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
$

在这种情况下经常被忽视的是 help 输出中的这一行:

如果你真的想添加它们,请使用 -f 选项。

使用 force 选项可以添加被忽略的文件:
$ 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
$

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接