git 没有二进制文件的 gitignore
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5711120/
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
gitignore without binary files
提问by JuanPablo
How can binary files be ignored in git
using the .gitignore
file?
git
使用.gitignore
文件时如何忽略二进制文件?
Example:
例子:
$ g++ hello.c -o hello
The "hello" file is a binary file. Can git
ignore this file ?
“hello”文件是一个二进制文件。可以git
忽略这个文件吗?
回答by VenomVendor
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
### Above combination will ignore all files without extension ###
# Ignore files with extension `.class` & `.sm`
*.class
*.sm
# Ignore `bin` dir
bin/
# or
*/bin/*
# Unignore all `.jar` in `bin` dir
!*/bin/*.jar
# Ignore all `library.jar` in `bin` dir
*/bin/library.jar
# Ignore a file with extension
relative/path/to/dir/filename.extension
# Ignore a file without extension
relative/path/to/dir/anotherfile
回答by manojlds
Add something like
添加类似的东西
*.o
in the .gitignore file and place it at the root of your repo ( or you can place in any sub directory you want - it will apply from that level on ) and check it in.
在 .gitignore 文件中,并将其放置在您的存储库的根目录中(或者您可以放置在您想要的任何子目录中 - 它将从该级别开始应用)并签入。
Edit:
编辑:
For binaries with no extension, you are better off placing them in bin/
or some other folder. Afterall there is no ignore based on content-type.
对于没有扩展名的二进制文件,最好将它们放在bin/
或其他文件夹中。毕竟没有基于内容类型的忽略。
You can try
你可以试试
*
!*.*
but that is not foolproof.
但这并非万无一失。
回答by icks
To append all executables to your .gitignore
(which you probably mean by "binary file" judging from your question), you can use
要将所有可执行文件附加到您的.gitignore
(从您的问题来看,您可能是指“二进制文件”),您可以使用
find . -executable -type f >>.gitignore
If you don't care about ordering of lines in your .gitignore
, you could also update your .gitignore
with the following command which also removes duplicates and keeps alphabetic ordering intact.
如果您不关心 中的行的顺序.gitignore
,您还可以.gitignore
使用以下命令更新您的 .
T=$(mktemp); (cat .gitignore; find . -executable -type f | sed -e 's%^\./%%') | sort | uniq >$T; mv $T .gitignore
Note, that you cannot pipe output directly to .gitignore
, because that would truncate the file before cat
opens it for reading. Also, you might want to add \! -regex '.*/.*/.*'
as an option to find if you do not want to include executable files in subdirectories.
请注意,您不能通过管道将输出直接传送到.gitignore
,因为这会在cat
打开文件进行读取之前截断文件。此外,您可能希望添加\! -regex '.*/.*/.*'
一个选项来查找是否不想在子目录中包含可执行文件。
回答by Andy White
Your best bet with binaries is to either give them an extension that you can easily filter out with a standard pattern, or put them into directories that you can filter out at the directory level.
二进制文件的最佳选择是给它们一个扩展名,您可以使用标准模式轻松过滤掉它们,或者将它们放入可以在目录级别过滤掉的目录中。
The extension suggestion is more applicable in Windows, because extensions are standard and basically required, but in Unix, you may or may not use extensions on your executable binaries. In this case, you can put them in a bin/ folder, and add bin/
to your .gitignore.
扩展建议在 Windows 中更适用,因为扩展是标准的并且基本上是必需的,但是在 Unix 中,您可能会或可能不会在可执行二进制文件上使用扩展。在这种情况下,您可以将它们放在 bin/ 文件夹中,然后添加bin/
到您的 .gitignore 中。
In your very specific, small-scope example, you can just put hello
in your .gitignore.
在您非常具体的小范围示例中,您只需hello
输入 .gitignore。
回答by Andrei Beliankou
You may try in your .gitignore
:
你可以试试你的.gitignore
:
*
!*.c
This approach has many disadvantages, but it's acceptable for small projects.
这种方法有很多缺点,但对于小型项目来说是可以接受的。
回答by vedantk
If you're using a makefile, you could try modifying your make rules to append the names of new binaries to your .gitignore file.
如果您使用的是 makefile,则可以尝试修改 make 规则以将新二进制文件的名称附加到 .gitignore 文件中。
Here's an example Makefile for a small Haskell project;
这是一个小型 Haskell 项目的 Makefile 示例;
all: $(patsubst %.hs, %, $(wildcard *.hs))
%: %.hs
ghc $^
grep -xq "$@" .gitignore || echo $@ >> .gitignore
This makefile defines a rule for creating executables out of Haskell code. After ghc is invoked, we check the .gitignore to see if the binary is already in it. If it isn't, we append the name of the binary to the file.
这个 makefile 定义了从 Haskell 代码中创建可执行文件的规则。调用 ghc 后,我们检查 .gitignore 以查看二进制文件是否已经在其中。如果不是,我们将二进制文件的名称附加到文件中。
回答by mattdlockyer
Binary files are often without extensions. If this is your case try this:
二进制文件通常没有扩展名。如果这是您的情况,请尝试以下操作:
*
!/**/
!*.*
回答by Tyler Earnest
Here's another solution using file. This way executable scripts will not end up in gitignore. You may need to change how the output from file is interpreted to match your system. One could then set up a pre-commit hook to call this script each time you commit.
这是使用文件的另一种解决方案。这样可执行脚本就不会出现在 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)
回答by shaman.sir
A way to also ignore in some subdir, not only in a root:
一种在某些子目录中也可以忽略的方法,而不仅仅是在根目录中:
# 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/**/*.*
Or, if you want to include only some specific types of files:
或者,如果您只想包含某些特定类型的文件:
/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h
Seems it may even also work like for every new subdirectory if you want!:
如果你愿意,它甚至可以像每个新的子目录一样工作!:
/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h
Leading slashes are important only in first two lines and optional in other. Tailing slash in !/*/
and !/subdir/
is also optional, but only in this line.
前导斜杠仅在前两行中很重要,在其他行中是可选的。尾部斜线 in !/*/
and!/subdir/
也是可选的,但仅限于这一行。
回答by Shaun Peretz
If you follow these commands on your .gitignore file and files still seems to appear you may want to try:
如果您在 .gitignore 文件上执行这些命令并且文件似乎仍然出现,您可能想尝试:
git rm --cached FILENAME
After that, add your .gitignore, commit and push. Took me 40 minutes to understand that, hope this helps to newbies like me
之后,添加您的 .gitignore,提交和推送。我花了 40 分钟才明白,希望这对像我这样的新手有所帮助