Make .gitignore ignore everything except a few files

前端 未结 23 2585
无人共我
无人共我 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 01:04

    There are a bunch of similar questions about this, so I'll post what I wrote before:

    The only way I got this to work on my machine was to do it this way:

    # Ignore all directories, and all sub-directories, and it's contents:
    */*
    
    #Now ignore all files in the current directory 
    #(This fails to ignore files without a ".", for example 
    #'file.txt' works, but 
    #'file' doesn't):
    *.*
    
    #Only Include these specific directories and subdirectories and files if you wish:
    !wordpress/somefile.jpg
    !wordpress/
    !wordpress/*/
    !wordpress/*/wp-content/
    !wordpress/*/wp-content/themes/
    !wordpress/*/wp-content/themes/*
    !wordpress/*/wp-content/themes/*/*
    !wordpress/*/wp-content/themes/*/*/*
    !wordpress/*/wp-content/themes/*/*/*/*
    !wordpress/*/wp-content/themes/*/*/*/*/*
    

    Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories 5 deep under themes, I still need to spell that out.

    This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153

    These were useful topics:

    • How do negated patterns work in .gitignore?
    • How do gitignore exclusion rules actually work?

    I also tried

    *
    */*
    **/**
    

    and **/wp-content/themes/**

    or /wp-content/themes/**/*

    None of that worked for me, either. Lots of trail and error!

提交回复
热议问题