I am working on a C project which generates lots of binaries. I try to keep the source under source control. However, I would like the binaries to be ignored without having to add them to the gitignore file whenever I create a new one. This is a sandbox of sorts and so I create lots of new binaries as I try out new things. I could build all my binaries in a single directory or give them a suffix, but I dislike these reasons. Since GCC automatically sets the executable bit on the binaries it produces, can I use that flag to ignore them?
问题:
回答1:
I recommend against this. What happens if you need to use a script in your build process or at runtime, but it's accidentally gitignored because it has the executable bit? This could especially create confusion if other people contribute to your project without knowing about the gitignore. You're best off manually adding files (or using a bin directory) to solve this problem.
That being said, gitignore does not seem to support mode-based file ignores, but you can automate the process of manually adding files that are executable.
If your .gitignore
file can be overwritten:
Run this to update your ignored files: find -type f -executable | sed 's#^.##' > .gitignore
If your .gitignore
file should be kept intact:
- Setup a new ignore file (your existing
.gitignore
should still work):git config core.excludesfile .auto_gitignore
- Run this to create/update your ignored file list:
find -type f -executable | sed 's#^.##' > .auto_gitignore
- Commit your
.auto_gitignore
if you want to share it with others, or add.auto_gitignore
to.git/info/exclude
if you don't. Note that if you do share it, your collaborators will also have to do step 1 individually.
Note: If you get an error because your version of find doesn't support -executable
, replace it with -perm /111
.
回答2:
Not directly, but you can use find
to find programs with execute permissions:
find . -type f -perm +0111 -print
Run as needed to create new .gitignore
entries. (Note that this is to be used with care; it will find executable shell scripts, which are source files, as well as executable binaries.)