如何创建 Git 属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18331048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a Git attributes file
提问by Carel
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.
我想描述一个属性文件添加到我的Git仓库在这里。它表明我应该包含.gitattributes
在我的存储库的根文件夹中。
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 ?
是
.gitattributes
一种简单的文件还是文件夹?我如何创建这个文件/文件夹?Windows 引发以下错误:
“无法创建没有文件名的文件”
“您必须输入文件名”
应该在哪里添加文件?在存储库根目录下,
X:\PATH\TO\Repository\
或 Git 文件夹下,X:\PATH\TO\Repository\.git\
?是否会为克隆/分叉我的存储库的其他贡献者强制执行这些属性?
例如,如果我推送到 GitHub 或 GitLab 并且其他一些人拉下副本,我添加的属性也会在他们的机器上强制执行吗?
Related :Git Ignore
相关:Git 忽略
Note :I'm using TortoiseGitand msysgit on a Windows platform if it helps.
注意:如果有帮助,我将在 Windows 平台上使用TortoiseGit和 msysgit。
回答by Carel
Is .gitattributes
a file or a folder ? How do I create it ?
是.gitattributes
文件还是文件夹?我如何创建它?
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 trailingDOT
for you producing the appropriate filenameUnix :and it's variants (Ubuntu, Raspberrian, Mac OS etc.)
touch .gitattributes
Alternatively :Clone the Git Attributesrepository and move/copy the
.gitattributes
file that is applicable to your repository (Footnote 1 and 2).
Windows:在Windows 资源管理器中创建一个新的文本文件(右键单击>新建>文本文件)并将其重命名(快捷方式:)
F2
如下.gitattributes.
注:也就是说
DOT
+gitattributes
+DOT
,Windows将删除尾随DOT
你产生适当的文件名Unix :及其变体(Ubuntu、Raspberrian、Mac OS 等)
touch .gitattributes
或者:克隆Git 属性存储库并移动/复制
.gitattributes
适用于您的存储库的文件(脚注 1 和 2)。
Where should one add the file ? Under the repository root, X:\PATH\TO\Repository\
, or the Git folder, X:\PATH\TO\Repository\.git\
?
应该在哪里添加文件?在存储库根目录下,X:\PATH\TO\Repository\
或 Git 文件夹下,X:\PATH\TO\Repository\.git\
?
There are two locations where one can add the Git attributes file
有两个位置可以添加 Git 属性文件
\Repository (1)
\Repository\.git\info\attributes (3)
- Adding a
.gitattributes
file here sets the attributes "Universally". - 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 reasonfor doing so .
- 在
.gitattributes
此处添加文件可设置“通用”属性。 - 在
.gitattributes
此处创建文件会设置“个人”属性,即自己机器上的存储库。虽然不可取,但您可能有充分的理由这样做。
Will the attributes be enforced for other contributors cloning/forking my repository ?
是否会为克隆/分叉我的存储库的其他贡献者强制执行这些属性?
Provided the file is committedto 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
.
如果文件被提交到存储库,它将在存储库被克隆时被克隆,并且相应地在一个项目的贡献者的机器上强制执行的属性(脚注:3 和 4)。属性在.gitattributes
添加到项目根目录时“全局”应用X:\PATH\TO\Repository\.gitignore
,当添加到某个子文件夹时“本地”应用X:\PATH\TO\Repository\SOME\SUB-FOLDER\.gitignore
。
Footnote(s) :
脚注:
- 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. - Coincidentally there is also repository for Git Ignore.
- 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. - 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 !!!
- 如果您使用大文件存储 (Git LFS),则必须小心,因为这些文件可能无法识别 LFS,反之亦然。此外
.gitattributes
,当您设置跟踪时,LFS 将为您创建。 - 巧合的是,还有Git Ignore 的存储库。
- 最初,当我第一次问这个问题时,我关心的是行尾的正确处理;
.gitattributes
处理得很好,但每个贡献者最好在他们自己的机器上正确配置 git。 - 不要成为添加
.gitattributes
到.gitignore
文件中的白痴。是的,这将应用“个人”属性,但有一天会有人“全局”覆盖您的解决方法,并且会有眼泪和拥抱......很多拥抱!