Search code examples
gitgitignore

How to setup gitignore to track specific folders/files multiple levels down?


I'm trying to set up a repository for multiple projects that uses Visual Studio.

I'm using a laptop and a desktop, but VS seems to have issues when stuff is on different drives (VS is installed on C drive for both computers, but on my desktop, my projects and external library are on the D drive while they are on the C drive for my laptop).

I want to track just the .cpp and .h source files, but I also want to keep the file structure so I don't have to manually move files around every time I pull from the repo, so I want to track the parent src folder and its parent (the project folder) as well. I don't want to have to add in another line to the gitignore for every project I add.

The files would be organized something like this...

-root
  -.gitignore
  -project1/
    -bin/
    -src/
      -main.cpp
      -foo.cpp
      -foo.h
    -project1.sln
    -project1.vcxproj
    -donttrack2.txt
  -project2/
    -bin/
    -src/
      -main.cpp
      -bar.cpp
      -bar.h
    -project2.sln
    -project2.vcxproj
  -donttrack.txt

I've tried looking at the documentation and another answer but I can't seem to get it working. Any help would be appreciated.


Solution

  • In your gitignore, you can first specify to ignore all files

    */*.*
    */*/*.*
    */*/*/*.*
    

    etcetera (do not do this with directories, though, because you cannot undo ignored directories)

    Afterwards, you undo the ignoring of files in src folders:

    !src/*
    !*/src/*
    !*/*/src/*