dylanfm
dylanfm

Reputation: 6345

Git ignore file for C projects

I've just started to learn C (using Thinking In C) and I'm wondering about what files I should be ignoring in a C project's git repository.

No suggestion can be too obvious -- I'm a total noob. Thanks!

Upvotes: 23

Views: 30579

Answers (5)

lauhub
lauhub

Reputation: 920

Using a *nix system and a Makefile, you could add each generated file to .gitignore.

As an example I use the following when creating an executable from a single source (Example for a C executable generation):

%: %.c
    gcc -o $@ $<
    grep '^$@$$' .gitignore > /dev/null || echo '$@' >> .gitignore

The following line can be added to other recipes to add target $@ to .gitignore file:

grep '^$@$$' .gitignore > /dev/null || echo '$@' >> .gitignore

Explanation:

  • grep '^$@$$' .gitignore : searches for the target in .gitignore
    • ^ indicates start of line
    • $$ is a single $ (but Makefile needs $$ to work) and indicates the end of the line
    • '^$@$$' represents the target name
  • || : executes the next command only if the left hand one failed
    • so only if grep ... does not find the target name in .gitignore, echo ... is executed
  • echo '$@' >> .gitignore: adds the target name to .gitignore

Eventually, you will add to clean and rebuild everything to make sure all files are correctly ignored

Upvotes: 0

Unmitigated
Unmitigated

Reputation: 89462

Github's .gitignore file templates cover most of the common files for projects in a variety of languages.

The C .gitignore template looks like this:

# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

Upvotes: 1

SpiRail
SpiRail

Reputation: 1415

I use this in my .gitignore But I am building for micro-controllers, so I don't know if it helps you much.

The easiest way to know, is just do a make clean, then add all your files, then do a make all and see what extra stuff appears.

#Some of these are related to eclipse. So i keep them out of my repo
.cproject
.dep/
.project
.settings/

#files being edited
*~

# make and build files
*.lst
*.o
*.eep
*.lss
*.map
*.sym

# I keep these, since I prefer having the reference of the final build
# *.elf
# *.hex

Upvotes: 4

Flame
Flame

Reputation: 2207

You can also setup your build to happen in a subdirectory say build and then you can ignore the whole thing inside .gitignore

build/

And you're done.

Upvotes: 17

RedBlueThing
RedBlueThing

Reputation: 42532

I guess there will be a few generated files that you don't wan't to be sticking in your repo (assuming your build output dir is in your git heirachy):

  • object files (.o, o.obj)
  • libraries (.lib)
  • DLLs, shared objects (.so, .dll)
  • Executables (.exe, a.out ?)

GIT ignore files are something I tend to do iteratively. "Hey, I don't need those things in my repo" ...

Edit: re dmckee's comment

Yep, you definately want to be ignoring swap files, temp files etc. I have the following as a baseline for my .gitignore:

  • *.swp
  • .~
  • thumbs.db

Upvotes: 19

Related Questions