本文介绍了用于在 init 上创建 .gitattributes 的 git 别名 - 如何添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最新版本

jinit= !git init && printf "* text=auto\n*.java text\n*.jsp text\n*.css text\n*.html text\n*.js text\n*.xml text\n*.sql text\n*.MF text\n*.tld text\n*.md text\n\n# git files\n*.gitignore text\n*.gitattributes text\n\n# eclipse files\n*.classpath text\n*.project text\n*.prefs text\n*.properties text\n*.component text\n" >> .gitattributes && echo "bin/" >> .gitignore && git add .

这实际上会尝试执行 gitattributes 中的行!更改 \n\r\n 将使脚本崩溃,而 \\\n 将成功运行脚本,尽管 gitattributes 将只包含

This will actually try to execute the lines in gitattributes ! Changing \n for \r\n will crash the script and for \\\n will successfully run the script albeit the gitattributes will only contain

*

那么我如何用新行回应这个?Windows、mingwin、msysgit

So how do I echo this with new lines ? Windows, mingwin, msysgit

我也试过单引号

编辑 2014.03.13:

jinit= !git init && `echo '* text=auto\n\n*.java text\n*.jsp text\n\
*.css text\n*.html text\n*.js text\n*.xml text\n*.sql text\n*.MF text\n\
*.tld text\n*.md text\n\n*.gitignore text\n*.gitattributes text\n\n\
*.classpath text\n*.project text\n*.prefs text\n*.properties text\n\
*.component text\n' >> .gitattributes` && `echo "bin/" >> .gitignore`

EDIT 2014.03.14:使用 \ 包装命令 - 仍然有效,但在 *.css、*.tld、*.classpath 和 * 之前引入了一个空格.组件.

EDIT 2014.03.14: using \ to wrap the command - still works but a space is introduced before *.css, *.tld, *.classpath and *.component.

似乎无法管理回声评论.因此,如果我添加 \n# git files\n\*.gitignore text\n (...) 它是 EOF - 任何解决方法?

Can't seem to manage echoing comments. So if I add \n# git files\n\*.gitignore text\n (...) it EOFs - any workaround for that ?

最后我使用了不同的方法(见评论):

Finally I used a different approach (see comments):

jinit= !git init\
    && `ln /c/Dropbox/_/_git/java_eclipse.gitattributes .gitattributes`\
    && `echo "bin/" >> .gitignore`\
    && git add .

但我暂时将其保留为句法部分

but I leave this open for now, for the syntactic part

推荐答案

有一条评论建议您只需将 .gitattributes 文件写入您的 git 模板目录.这是行不通的,因为在 文档 中,它特别指出:

There was a comment suggesting that you simply write a .gitattributes file into your git templates directory. This would not work because in the documentation, it specifically says:

模板目录中名称不以点开头的文件和目录在创建后将被复制到$GIT_DIR中.

然而,我们实际上可以利用这一点!你说

However, we can actually use this to our advantage! You stated that

我想为不同的语言使用不同的初始化别名(以及 gitignore/attrs)

这是一个公平的观点!但是,我仍然认为您当前的方法过于复杂.我建议使用 any 名称将文件添加到您的模板目录中,名称以点开头,以便您识别它.例如,您可以选择 .gitattributes-english.gitattributes-french.

which is a fair point! However, I still believe your current approach is overly complicated. I would suggest adding files to your template directory with any name that begins with a dot which will allow you to identify it. For example, you might choose .gitattributes-english and .gitattributes-french.

现在,运行以下命令:

git config --global alias.init-english "!git init;cp $(git config init.templatedir)/.gitattributes-english ./.gitattributes"

git config --global alias.init-french "!git init;cp $(git config init.templatedir)/.gitattributes-french ./.gitattributes"

现在,git init-english 将初始化一个仓库,并将 .gitattributes-english 的内容从你的模板目录复制到 .gitattributes> 在新存储库中,并且不会.gitattributes-french 添加到新存储库中.git init-french 将改为使用 .gitattributes-french 而不是 .gitattributes-english.

Now, git init-english will initialize a repository, and copy the contents of .gitattributes-english from your template directory to .gitattributes in the new repository and will not add .gitattributes-french to the new repository. git init-french will instead use the .gitattributes-french and not .gitattributes-english.

我认为这还有一个额外的好处,即允许您编辑特定的 .gitattributes 文件而无需更新别名!

I would argue that this also has the added benefit of allowing you to edit the specific .gitattributes files without having to update the alias!

您显然不必使用 init.templatedir 我只是发现使用它最有意义,因为这些是模板.

You obviously do not have to use the init.templatedir I just find that it makes the most sense to use it as these are templates.

最后请注意,这适用于 Windows!

Note finally that this works on Windows!

这篇关于用于在 init 上创建 .gitattributes 的 git 别名 - 如何添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 13:03