How to create a Git attributes file

我只是一个虾纸丫 提交于 2019-12-17 22:46:53

问题


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.


回答1:


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

  • Linux : and it's variants (Gentoo, Raspberry Pi, 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 !!!


来源:https://stackoverflow.com/questions/18331048/how-to-create-a-git-attributes-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!