git ignoring .gitattributes pattern
Asked Answered
R

7

10

I've a directory structure like this:

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

Edit: to further clarify the above, directories have a trailing / and children are indented beneath a directory, so blah and more_blah are directories but Rakefile and .gitattributes are files, but all four are children of Site.


I'm running git-archive from the Site directory like so:

git archive --format=tar --prefix=git-v0.0.1/ v0.0.1 | gzip > ../deploy/git-v0.0.1.tar.zip

but whatever pattern I put in .gitattributes, the resulting archive always contains Rakefile. I've tried:

  • Rakefile
  • Site/Rakefile
  • */Rakefile
  • ./Rakefile
  • Rakefile*
  • *

None of them work as I'd expect. Is anyone willing to point out the obvious yet non-obvious to me solution? Any help is much appreciated.


My apologies for not being clear.

  • I said the pattern I was using didn't seem to work, but I am using "export-ignore" after the pattern.
  • Rakefile is not a directory, just a file
  • The .gitattributes file is successful in removing other patterns from the archive, Rakefile is not the only pattern used, but is the only one that doesn't work. It doesn't work whether I have it on its own or with other patterns, and at any place in the file. This is not true, due to having renamed certain files but not archiving the commit with the rename I was appearing to get some good results. My bad! :S

This is my .gitattributes (sitting in the directory Site)

Rakefile        export-ignore
*.local         export-ignore
*.development   export-ignore
*.staging       export-ignore
Roof answered 2/4, 2011 at 17:35 Comment(1)
How does your complete .gitattributes file look like?Hipparchus
R
5

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:


First edit:

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 (note: dead link)

... (normally the root of your project)


Much later edit:

Even the latest version of the docs (2.35.1) there are references to .gitattributes files that aren't in the root of the repo (GIT_WORK_TREE / GIT_DIR) e.g.

not in .gitattributes files in working tree subdirectories

Git does not follow symbolic links when accessing a .gitattributes file in the working tree.


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.

Roof answered 11/4, 2011 at 16:24 Comment(3)
Thank you for providing a correct answer to your question. The other answers were not helpful in my case.Flytrap
The link seems to be broken...Dulcedulcea
@Dulcedulcea Thanks. I tried to fix the answer to take that into account.Roof
W
6

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.

Weeks answered 23/5, 2014 at 6:12 Comment(2)
Wouldn't tests/** export-ignore work in this case?Succotash
tests/**/* is what you need to ignore all files of any depth from the directory 'tests.'Squiggle
R
5

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:


First edit:

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 (note: dead link)

... (normally the root of your project)


Much later edit:

Even the latest version of the docs (2.35.1) there are references to .gitattributes files that aren't in the root of the repo (GIT_WORK_TREE / GIT_DIR) e.g.

not in .gitattributes files in working tree subdirectories

Git does not follow symbolic links when accessing a .gitattributes file in the working tree.


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.

Roof answered 11/4, 2011 at 16:24 Comment(3)
Thank you for providing a correct answer to your question. The other answers were not helpful in my case.Flytrap
The link seems to be broken...Dulcedulcea
@Dulcedulcea Thanks. I tried to fix the answer to take that into account.Roof
M
4

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
Marnamarne answered 2/4, 2011 at 18:56 Comment(4)
Also, be sure to either commit your .gitattributes or use the --worktree-attributes option for git archive. By default, it uses the attributes from the tree that's being archived; if they're just in the working directory, they'll normally be ignored. To test, you might use --worktree-attributes, then commit it once you're sure it works.Teniers
@Jefromi - I used the instructions in the Git book but they don't say you have to check in the file for it to be used "These path-specific settings are called Git attributes and are set either in a .gitattributes file in one of your directories (normally the root of your project) or in the .git/info/attributes file if you don’t want the attributes file committed with your project." But, now you've brought it up, I see the git-archive man page suggests the --worktree-attributes option to make this work. I'll try that now.Roof
@Jefromi - Ok, two things were needed. --work-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. Anyway, I consider your comment to be the correct answer, so if you'd like to make that comment into an answer I can mark it so.Roof
Cannot add the trail '/' in version 1.7.5.4Precedent
E
3

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/*
Emanuele answered 27/2, 2020 at 20:53 Comment(0)
K
2

With git version 1.7.2.5, which is the default on debian squeeze (hence this post), there must not be a slash at the end to ignore a directory. So in order to ignore the deploy dir in the above question the following line must be used (no slash):

deploy        export-ignore

This is in contradiction to the documentation (man gitattributes / man gitignore) and the git book. The documentation of gitattributes references gitignore. Interestingly dirs with a slash (deploy/) work in a .gitignore file.

I did not test later versions of git.

Kinzer answered 21/8, 2012 at 17:54 Comment(1)
With git 1.8.1.1 this is not the case. If you do not include the trailing slash the dir will be added to the archive.Electrophone
S
2

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.

Solo answered 16/10, 2013 at 23:31 Comment(0)
L
1

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
Lord answered 2/4, 2011 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.