Make .gitignore ignore everything except a few files

前端 未结 23 2908
无人共我
无人共我 2020-11-22 00:14

I understand that a .gitignore file cloaks specified files from Git\'s version control. I have a project (LaTeX) that generates lots of extra files (.auth, .dvi, .pdf, logs,

23条回答
  •  轮回少年
    2020-11-22 00:40

    You want to use /* instead of * or */ in most cases

    Using * is valid, but it works recursively. It won't look into directories from then on out. People recommend using !*/ to whitelist directories again, but it's actually better to blacklist the highest level folder with /*

    # Blacklist files/folders in same directory as the .gitignore file
    /*
    
    # Whitelist some files
    !.gitignore
    !README.md
    
    # Ignore all files named .DS_Store or ending with .log
    **/.DS_Store
    **.log
    
    # Whitelist folder/a/b1/ and folder/a/b2/
    # trailing "/" is optional for folders, may match file though.
    # "/" is NOT optional when followed by a *
    !folder/
    folder/*
    !folder/a/
    folder/a/*
    !folder/a/b1/
    !folder/a/b2/
    !folder/a/file.txt
    
    # Adding to the above, this also works...
    !/folder/a/deeply
    /folder/a/deeply/*
    !/folder/a/deeply/nested
    /folder/a/deeply/nested/*
    !/folder/a/deeply/nested/subfolder
    

    The above code would ignore all files except for .gitignore, README.md, folder/a/file.txt, folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders. (And .DS_Store and *.log files would be ignored in those folders.)

    Obviously I could do e.g. !/folder or !/.gitignore too.

    More info: http://git-scm.com/docs/gitignore

提交回复
热议问题