Search code examples
gitgithubgitignore

How can I ignore everything but *.cpp files


So I have a file directory that contains many types of files, but I would like to ignore every file except a few and any *.cpp. Here is what I have in my .gitignore, but the *.cpp is not being committed.

# Ignore Everything
*

# But not .cpp
!*.cpp
!.gitignore
!README.md

File Directory


Solution

  • You can "un-ignore" directories using !*/ so that it tries adding that directory. But since git has no tracking for directories it will still have to find files to add. Your ignore all * and don't ignore cpp !*.cpp will do the trick

    Modify your .gitignore to be:

    # Ignore Everything
    *
    
    # But not .cpp
    !*.cpp
    # Or directories
    !*/
    !.gitignore
    !README.md
    

    The basic idea is git tracks files not directories.