10

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?

John Weldon
  • 39,849
  • 11
  • 94
  • 127
Julien Chien
  • 2,052
  • 4
  • 16
  • 32

3 Answers3

11

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.

Craig Brown
  • 1,891
  • 1
  • 24
  • 25
5

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.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

By default, the Golang compiler will name your binary after the folder name containing your .go source files. In your IDE you can specify the golang compile command with build opts. In my case I have specified my IDE to run the following when I compile my .go source files on OSX:

"env GOOS=darwin GOARCH=amd64 go build -v -o bin/$(basename $(pwd)) && go test -v && go vet"

Notice that in my go build I specify the binary output path with -o and tell the compiler to write all binaries to the subfolder bin/ in the current directory and to name the binary with the name of the project folder. Then in your .gitignore file located in your project folder, add bin/* and git will ignore all the files in your bin/ subfolder.

Jun_in_Jeju
  • 11
  • 1
  • 2