22

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!

5 Answers 5

18

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
2
  • If you editor makes automatic backups (ala emacs' *~ files), you can probably do without those too. May 11, 2009 at 3:42
  • 2
    Usually you put ignoring of generated files in version controlled and transferred .gitignore file (so everybody will have generated files ignored), but ignoring backup files and other types of files depending on your environment (backup files can be *~ or *.bak) in not transferred repository .git/info/exclude (or in core.excludesfile set in ~/.gitconfig). May 11, 2009 at 9:52
18

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.

4

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
1

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
1
  • Nice! I recommend this as these templates are a result of the collective effort of highly skilled members of the programming community. Nov 17, 2022 at 19:40
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.