git ignoring .gitattributes pattern

后端 未结 7 2085
夕颜
夕颜 2020-12-16 20:45

I\'ve a directory structure like this:

root/
  .git
  deploy/
  Site/
    blah/
    more_blah/
      something.local
      else.development
    Rakefile
             


        
相关标签:
7条回答
  • 2020-12-16 21:27

    For directories I had problems with different versions of git, so I had to include an entry both with and without a training slash:

    • foo/bar export-ignore
    • foo/bar/ export-ignore

    Where foo/bar is relative to the where I ran git archive, but the actual .gitattributes was in the project root directory in addition to --worktree-attributes as noted above.

    0 讨论(0)
  • 2020-12-16 21:32

    Note: to ignore a directory, you needs to have a '/' at the end of said directory.

    Rakefile/
    

    For archive, like Arrowmaster mentions in his answer, and like the Pro Git book details, you need the export-ignore option:

    Rakefile/ export-ignore
    
    0 讨论(0)
  • 2020-12-16 21:33

    If you want git to ignore a file put it in the .gitignore file not the .attributes file.

    If you want to ignore your Rakefile put the following in the .gitignore file at the root of your project.

    /**/Rakefile
    

    Specify the full path if you have more than one and you only want to ignore one of them.

    A few examples of how to implement pattern matching for these files:

    For All Files in All Folders
    /**/*.ext
    
    For A File in All Folders
    /**/some_file.ext
    
    File in Root Folder
    /some_file.ext
    
    File in A Folder
    /some_folder/some_file.ext
    
    All Files in A Folder
    /some_folder/*
    
    0 讨论(0)
  • 2020-12-16 21:34

    I believe @Jefromi gave the information needed for me to resolve this with his comments, but is too humble to take the credit, and I'd like to keep my acceptance rating at 100% (quite rightly) so I'll give the answer here:


    Ok, two things were needed. --worktree-attributes on its own did not work, but when I moved the .gitattributes file into the root dir from the Site dir, then it worked. Again, the Git book implies that the file doesn't need to be in the root for it to work "... (normally the root of your project)", so I feel a bit let down by those docs (for once). I also think it's not-what-you'd-think behaviour to have to opt in the file when .gitignore just works, IMO.

    0 讨论(0)
  • 2020-12-16 21:39

    Not sure this is common case but I had trouble excluding folder tests from source tree which have many nested levels of folders. If I wrote only this line to .gitattributes

    tests/* export-ignore
    

    It didnt work and entire directory was remain in archive. The solution was to add wildcards for all subdirs levels:

    tests/* export-ignore
    tests/*/* export-ignore
    tests/*/*/* export-ignore
    tests/*/*/*/* export-ignore
    tests/*/*/*/*/* export-ignore
    

    With these lines the tests directory finally disappeared from archive.

    0 讨论(0)
  • 2020-12-16 21:45

    Are you trying to have the files included in the repository but not in the archive created from git archive? If so the syntax of your .gitattributes files wrong. If not then .gitattributes is not what you should be using.

    To have files excluded from the archive produced by git archive you should put the following into the .gitattrubutes.

    Rakefile export-ignore
    
    0 讨论(0)
提交回复
热议问题