How to create a Git attributes file

Carel picture Carel · Aug 20, 2013 · Viewed 29.8k times · Source

I would like to add an attributes file to my Git repository as described here. It indicates that I should include .gitattributes in the root folder of my repository.

  • Is .gitattributes simply a file or is it a folder ? How do I create this file/folder ?

    Windows raises the following errors :

    "Can't create a file without a filename"

    "You must type a filename"

  • Where should one add the file ? Under the repository root, X:\PATH\TO\Repository\, or the Git folder, X:\PATH\TO\Repository\.git\ ?

  • Will the attributes be enforced for other contributors cloning/forking my repository ?

    e.g. If I pushed to GitHub or GitLab and some other party pulled down a copy would the attributes I added be enforced upon their machine as well ?

Related : Git Ignore

Note : I'm using TortoiseGit and msysgit on a Windows platform if it helps.

Answer

Carel picture Carel · Aug 20, 2013

Is .gitattributes a file or a folder ? How do I create it ?

  • Windows : Create a new text file (Right click>new>text file) within windows explorer and rename it (Shortcut : F2) as follows

    .gitattributes.
    

    Note : That is DOT + gitattributes + DOT, windows will remove the trailing DOT for you producing the appropriate filename

  • Unix : and it's variants (Ubuntu, Raspberrian, Mac OS etc.)

    touch .gitattributes
    
  • Alternatively : Clone the Git Attributes repository and move/copy the .gitattributes file that is applicable to your repository (Footnote 1 and 2).

Where should one add the file ? Under the repository root, X:\PATH\TO\Repository\, or the Git folder, X:\PATH\TO\Repository\.git\ ?

There are two locations where one can add the Git attributes file

    \Repository                        (1)
    \Repository\.git\info\attributes   (3)
  1. Adding a .gitattributes file here sets the attributes "Universally".
  2. Creating the .gitattributes file here sets the attributes "Personally", that is the repository upon one's own machine. While inadvisable you may a have good reason for doing so .

Will the attributes be enforced for other contributors cloning/forking my repository ?

Provided the file is committed to the repository, it will be cloned when the repository is cloned and the attributes enforced upon the machines of the the contributors of ones project accordingly (Footnote : 3 and 4). Attributes are applied "Globally " when .gitattributes is added to the project root, X:\PATH\TO\Repository\.gitignore, and "Locally" when added to some sub-folder, X:\PATH\TO\Repository\SOME\SUB-FOLDER\.gitignore.


Footnote(s) :

  1. Some care must be taken if you are using Large File Storage (Git LFS) as these files may not be LFS aware or vice versa. Additionally LFS will create .gitattributes for you when you setup tracking.
  2. Coincidentally there is also repository for Git Ignore.
  3. Initially, when I first asked the question, I was concerned about the correct treatment of line endings; .gitattributes handles this well but it is better for each contributor to configure git properly upon their own machines.
  4. Don't be the idiot that adds .gitattributes to the .gitignore file. Yes this will apply the attributes "Personally" but some one will overwrite your workaround "Globally" one day and there will be tears and hugging... a lot of hugging !!!