Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Git attributes file

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.

like image 963
Carel Avatar asked Aug 20 '13 09:08

Carel


People also ask

What is a git attributes file?

DESCRIPTION. A gitattributes file is a simple text file that gives attributes to pathnames. Each line in gitattributes file is of form: pattern attr1 attr2 ... That is, a pattern followed by an attributes list, separated by whitespaces.

Where is git attributes file?

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.

How do I open a .gitattributes file?

If you cannot open your GITATTRIBUTES file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a GITATTRIBUTES file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

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 !!!
like image 169
Carel Avatar answered Oct 05 '22 22:10

Carel