如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
当前回答
如果你在你的.gitignore文件上执行这些命令,文件仍然出现,你可能想尝试:
git rm --cached FILENAME
之后,添加你的。gitignore,提交并推送。 我花了40分钟才明白,希望这对像我这样的新手有帮助
其他回答
只需添加hello或/hello到你的.gitignore。要么工作。
二进制文件通常没有扩展名。如果这是你的情况,试试这个:
*
!/**/
!*.*
裁判:https://stackoverflow.com/a/19023985/1060487
要将所有可执行文件追加到.gitignore(从您的问题判断,您可能指的是“二进制文件”),可以使用
find . -executable -type f >>.gitignore
如果您不关心.gitignore中的行顺序,您还可以使用以下命令更新.gitignore,该命令还可以删除重复项并保持字母顺序不变。
T=$(mktemp); (cat .gitignore; find . -executable -type f | sed -e 's%^\./%%') | sort | uniq >$T; mv $T .gitignore
注意,不能将输出直接输送到.gitignore,因为这会在cat打开文件以供读取之前截断该文件。此外,您可能还想添加\!正则表达式”。* / * /。如果您不希望在子目录中包含可执行文件,则可以将*'作为查找选项。
这是另一个使用文件的解决方案。这样,可执行脚本就不会在gitignore中结束。您可能需要更改文件输出的解释方式,以匹配您的系统。然后可以设置一个预提交钩子,以便在每次提交时调用此脚本。
import subprocess, os
git_root = subprocess.check_output(['git', 'root']).decode("UTF-8").strip()
exes = []
cut = len(git_root)
for root, dirnames, filenames in os.walk(git_root+"/src/"):
for fname in filenames:
f = os.path.join(root,fname)
if not os.access(f,os.X_OK):
continue
ft = subprocess.check_output(['file', f]).decode("UTF-8")
if 'ELF' in ft and 'executable' in ft:
exes.append(f[cut:])
gifiles = [ str.strip(a) for a in open(git_root + "/.gitignore").readlines() ]
gitignore=frozenset(exes+gifiles)
with open(git_root+"/.gitignore", "w") as g:
for a in sorted(gitignore):
print(a, file=g)
在某些子目录中也可以忽略,而不仅仅是在根目录中:
# Ignore everything in a root
/*
# But not files with extension located in a root
!/*.*
# And not my subdir (by name)
!/subdir/
# Ignore everything inside my subdir on any level below
/subdir/**/*
# A bit of magic, removing last slash or changing combination with previous line
# fails everything. Though very possibly it just says not to ignore sub-sub-dirs.
!/subdir/**/
# ...Also excluding (grand-)children files having extension on any level
# below subdir
!/subdir/**/*.*
或者,如果你只想包含一些特定类型的文件:
/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h
如果你想的话,它甚至可以像每个新子目录一样工作!:
/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h
前导斜杠只在前两行中重要,在其他行中是可选的。在!/*/和!/subdir/中的尾斜杠也是可选的,但仅在这一行中。