Make .gitignore ignore everything except a few files

前端 未结 23 2570
无人共我
无人共我 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:00

    Gist

    # Ignore everything
    *
    
    # But not these files...
    !script.pl
    !template.latex
    

    And probably include:

    !.gitignore
    

    Reference

    From https://git-scm.com/docs/gitignore:

    An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".

    ...

    Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar
    
    0 讨论(0)
  • 2020-11-22 01:01

    If you want to ignore the whole content of a directory except one file inside it, you could write a pair of rules for each directory in the file path. E.g. .gitignore to ignore the pippo folder except from pippo/pluto/paperino.xml

    pippo/*
    !pippo/pluto
    pippo/pluto/*
    !pippo/pluto/paperino.xml
    

    Note that if you simply had written above:

    pippo/*
    !pippo/pluto/paperino.xml
    

    It wouldn't work because the intermediary pluto folder would not exist to Git, so paperino.xml could not find a place in which to exist.

    0 讨论(0)
  • 2020-11-22 01:03

    I have Jquery and Angular from bower. Bower installed them in

    /public_html/bower_components/jquery/dist/bunch-of-jquery-files
    /public_html/bower_components/jquery/src/bunch-of-jquery-source-files
    /public_html/bower_components/angular/angular-files
    

    The minimized jquery is inside the dist directory and angular is inside angular directory. I only needed minimized files to be commited to github. Some tampering with .gitignore and this is what I managed to conjure...

    /public_html/bower_components/jquery/*
    !public_html/bower_components/jquery/dist
    /public_html/bower_components/jquery/dist/*
    !public_html/bower_components/jquery/dist/jquery.min.js
    /public_html/bower_components/angular/*
    !public_html/bower_components/angular/angular.min.js
    

    Hope someone could find this useful.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-22 01:04

    This is how I did it:

    # Ignore everything
    *
    
    # Whitelist anything that's a directory
    !*/
    
    # Whitelist some files
    !.gitignore
    
    # Whitelist this folder and everything inside of it
    !wordpress/wp-content/themes/my-theme/**
    
    # Ignore this folder inside that folder
    wordpress/wp-content/themes/my-theme/node_modules
    
    # Ignore this file recursively
    **/.DS_Store
    

    Use gig status -u to view individual files in untracked directories recursively - with git status you'd only see folders, which could fool you into thinking that everything inside them was tracked

    0 讨论(0)
提交回复
热议问题