5

之前有一篇关于自动添加 .gitignore的文章,但是你如何在git init一个选项.gitignore中指定在 git init 上添加的对应项?

(换句话说,能够指定使用哪个.gitignore

这是否可以单独使用 git 命令或一些 shell 脚本?

4

1 回答 1

5

解决方案 1

如果您想依赖模板,如您链接的帖子中所示,使用模板目录,您应该有不同的模板目录,每个目录都有其正确的info/exclude文件,并使用

git init -c --template=/path/to/template

请注意,这不会创建一个.gitignore文件,而是一个.git/info/exclude文件,该文件保留在此 repo 的本地,并且在推送时不会被共享(不是这种情况.gitignore

解决方案 2

如果您不想费心创建目录,而是想从 GitHub 中获取预定义的目录,则.gitignore此快速脚本会启动一个 repo,从 GitHub 的预定义目录中获取 gitignore,并使用它进行第一次提交。

在此处查看可用的语言,并注意首字母必须大写。

#!/bin/sh

git init .
curl -o .gitignore --fail --show-error --silent --location https://raw.github.com/github/gitignore/master/$1.gitignore
git add .gitignore && git commit -m "added gitignore from GitHub"

调用 script git-init-more.shchmod +x它,并将它放在你的路径中,然后你可以像这样调用它:

$ git init-more Perl
于 2013-07-29T19:49:43.267 回答