How to gitignore Go binaries?

Viewed 10876

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?

3 Answers

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.

Related