我想通过自动创建.gitignore,README,LICENSE和其他git init
命令文件来优化我的git工作流程.
要做到这一点,我git init
在http://git-scm.com/docs/git-init的 RTFM,它告诉我做以下其中一项:
git init --template=<template_directory>
,但这很麻烦.init.templatedir
配置变量.现在我们正在谈论!所以我sudo mkdir /usr/share/git-core/templates/my_template
和touch
它中的一些文件,然后我vim ~/.gitconfig
和追加:
[init]
templatedir = /usr/share/git-core/templates/my_template
Run Code Online (Sandbox Code Playgroud)
并git config -l
告诉我:
...
init.templatedir =/usr/share/git-core/templates/my_template
...
对自己感到高兴,我去了我的开发游乐场目录,并且:
$ git init
Initialized empty Git repository in /the/current/directory
$ ls -a
. .. .git
Run Code Online (Sandbox Code Playgroud)
糟糕......文件在哪里?:(
快速检查:
$ ls -a /usr/share/git-core/templates/my_template
. .. .gitignore LICENSE README.md
$ git --version
git version 1.8.2.1
Run Code Online (Sandbox Code Playgroud)
它似乎$ git init --template=/usr/share/git-core/templates/my_template
也无效.
那么我在这里做错了什么呢?配置指令不正确?错误的模板或其位置(我在OSX上)?模板应该是git repo吗?一个裸的?
Tux*_*ude 21
您看到的git
行为是预期的行为:
如果您正确阅读了有关模板目录的手册:
模板目录
模板目录包含将在创建后复制到$ GIT_DIR的文件和目录.
这是从模板目录复制的文件放置在您的GIT_DIR
其中默认为.git
你的回购的根目录下的目录.
git init
据我所知,不支持工作树的模板.如果需要这种行为,您应该能够编写一些简单的bash别名或函数来为您完成此操作.
你可以做到,但需要一些额外的步骤.
创建您的默认目录结构,就像它是一个正常的repo:
mkdir template && cd template
git init && touch README.md && cat ~/.gitignore_global > .gitignore
git add --all && git commit -m "init"
Run Code Online (Sandbox Code Playgroud)(严格来说,这里最后一次提交不是必需的,但你最终必须这样做,所以为什么不现在)
现在删除您的工作树并移动.git文件:
mv .git/* ./ && rm -r README.md .gitignore .git
Run Code Online (Sandbox Code Playgroud)您现在可以将其设置为默认模板,但为了示例:
mkdir ../myrepo && cd ../myrepo
git init --template=../template
Run Code Online (Sandbox Code Playgroud)
(注有趣的消息:Reinitialized existing Git repository...
)
现在重要的一步:(您的仓库是最新的,但您的工作树不是):
git reset --hard
Run Code Online (Sandbox Code Playgroud)
(如果您之前跳过提交,则必须在重置前提交)
将来,假设您已经设置了默认模板,那么您就是这么简单
git init && git reset --hard
Run Code Online (Sandbox Code Playgroud)
(我没有直接的参考,但本章肯定有帮助.)
您可以创建一个模板存储库,每次要创建一个新项目时都可以对其进行克隆。然后,您删除.git文件夹并将内容复制到新项目中,然后再调用git init。假设您的模板存储库称为project_template:
$ git clone project_template new_project
$ cd new_project
$ rm -rf .git
$ git init
Run Code Online (Sandbox Code Playgroud)
不是理想的解决方案,但是可以编写脚本。
这是此处提供的步骤的修改,特定于使用GitHub。