Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gitignore Go binaries?

Tags:

git

gitignore

I have a .gitignore file like this:

# no binaries
*/
!*.go/
!.gitignore

I thought */ means to ignore all files in all subdirectories (so every file), !*.go/ means to not-ignore all *.go files in all subdirectories, and !.gitignore means to not ignore .gitignore.

However, the issue I have now is that when I create a new *.go file in a subdirectory, it's now ignored.

How do I correctly gitignore all compiled binaries, but not ignore *.go files?

I now have

**/*  
!**/*.go
!.gitignore

But it still ignores all *.go files in the ch1 directory. Anyone else have ideas?

like image 438
Julien Chien Avatar asked Mar 15 '16 17:03

Julien Chien


2 Answers

This will ignore everything except .go files, and works for subdirectories too:

**/*
!**/*.go
!**/

You may also want to check out this question, which is asking something very similar.

like image 59
Craig Brown Avatar answered Oct 06 '22 18:10

Craig Brown


You need to use:

**/*.go

The ** is for ignoring files inside any folder and not only in the current folder.


A minor bug was fixed in git v2.7:

Allow a later !/abc/def to override an earlier /abc that
appears in the same .gitignore file to make it easier to express
everything in /abc directory is ignored, except for ....


From the .gitignore documentation:

Two consecutive asterisks (**) in patterns matched against full pathname may have special meaning:

Leading **

A leading ** followed by a slash means match in all directories.
For example, **/foo matches file or directory foo anywhere, the same as pattern foo.
**/foo/bar matches file or directory "bar" anywhere that is directly under directory foo.

Trailing **

A trailing /** matches everything inside.
For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth.

/**/

A slash followed by two consecutive asterisks then a slash matches zero or more directories.
For example, a/**/b matches a/b, a/x/b, a/x/y/b and so on.

like image 43
CodeWizard Avatar answered Oct 06 '22 19:10

CodeWizard