Search code examples
c++gitgithububuntu-18.04gitignore

Adding raw files to .gitignore


I created a git repository folder which contains all of my C++ code. When I compile the code with g++ -g filename.cpp -o filename -lgraph in my Ubuntu 18.04, it creates a filename file without extension. I want git to stop tracking all these created files while I do git add . and track the rest. What should I add to the .gitignore file so that these files are ignored?


Solution

  • I would suggest to use an build Folder outside the source Folder.

    But if you really need the output executable to be in the source Folder you can add following to .gitignore:

    *
    !*.cpp
    !*.h
    

    If you do so, git ignores all files which are not cpp or c-Files

    • * git ignores all files
    • *.cpp but does not ignore cpp files
    • *.h but does not ignore h files

    Depending on other files you have in your source Folder you have to exclude them too.

    But it is better to have an dedicated build folder.

    Edit: You can even have the build folder inside your source folder and exclude everything inside this folder.

    build/*
    

    Make sure the build folder exists

    mkdir build
    

    and modify your call to g++

    g++ -g filename.cpp -o build/filename -lgraph