How to git ignore subfolders / subdirectories?

后端 未结 10 1157
孤街浪徒
孤街浪徒 2020-11-28 00:30

I have a lot of projects in my .Net solution. I would like to exclude all \"bin/Debug\" and \"bin/Release\" folders (and their contents), but still include the \"bin\" folde

相关标签:
10条回答
  • 2020-11-28 00:43

    The question isn't asking about ignoring all subdirectories, but I couldn't find the answer anywhere, so I'll post it: */*.

    0 讨论(0)
  • 2020-11-28 00:44

    The generic way to ignore all subfolders, while continuing to track the files that are in the /bin directory would be to add the following line to your project's .gitignore file:

    bin/*/*
    

    If you want to ignore only particular named subfolders, you can do:

    bin/Debug/*
    bin/Release/*
    

    nb. if the bin directory is not in the root of your project (alongside the .gitignore file), then instead of eg. bin/*/* you might need path/to/bin/*/*

    0 讨论(0)
  • 2020-11-28 00:45

    Have you tried wildcards?

    Solution/*/bin/Debug
    Solution/*/bin/Release
    

    With version 1.8.2 of git, you can also use the ** wildcard to match any level of subdirectories:

    **/bin/Debug/
    **/bin/Release/
    
    0 讨论(0)
  • 2020-11-28 00:52

    To exclude content and subdirectories:

    **/bin/*
    

    To just exclude all subdirectories but take the content, add "/":

    **/bin/*/
    
    0 讨论(0)
  • 2020-11-28 00:54

    To ignore all subdirectories you can simply use:

    **/
    

    This works as of version 1.8.2 of git.

    0 讨论(0)
  • 2020-11-28 00:56

    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:
    !wordpress/
    !wordpress/*/
    !wordpress/*/wp-content/
    !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)
提交回复
热议问题