在 git init 上设置默认目录结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16363460/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:29:46  来源:igfitidea点击:

Set up a default directory structure on git init

gittemplates

提问by Oleg

I would like to optimise my git workflow by automating creation of .gitignore, README, LICENSE and other files on git initcommand.

我想通过根据命令自动创建 .gitignore、README、LICENSE 和其他文件来优化我的 git 工作流程git init

To do that I RTFM of git initat http://git-scm.com/docs/git-initand it tells me to do one of the following:

为此,我git inithttp://git-scm.com/docs/git-init 上进行RTFM,它告诉我执行以下操作之一:

  1. Use git init --template=<template_directory>, but it's bothersome.
  2. Alter the contents of the $GIT_TEMPLATE_DIR environment variable, but I would rather not.
  3. Set the init.templatedirconfiguration variable. Now we're talking!
  1. 使用git init --template=<template_directory>,但很麻烦。
  2. 更改 $GIT_TEMPLATE_DIR 环境变量的内容,但我宁愿不要。
  3. 设置init.templatedir配置变量。现在我们在说话!

So I sudo mkdir /usr/share/git-core/templates/my_templateand touchsome files in it, then I vim ~/.gitconfigand append:

所以我sudo mkdir /usr/share/git-core/templates/my_templatetouch其中的一些文件,然后我vim ~/.gitconfig并附加:

[init]
    templatedir = /usr/share/git-core/templates/my_template

And git config -ltells me:

git config -l告诉我:

...
init.templatedir=/usr/share/git-core/templates/my_template
...

...
init.templatedir=/usr/share/git-core/templates/my_template
...

Happy with myself, I go to my development playground directory and:

对自己很满意,我转到我的开发场所目录,然后:

$ git init
Initialized empty Git repository in /the/current/directory
$ ls -a
.   ..  .git

Bummer... where are the files? :(

无赖……文件在哪里?:(

Quick check:

快速检查:

$ ls -a /usr/share/git-core/templates/my_template
.   ..  .gitignore  LICENSE README.md
$ git --version
git version 1.8.2.1

It seems that $ git init --template=/usr/share/git-core/templates/my_templatedoesn't work either.

好像$ git init --template=/usr/share/git-core/templates/my_template也行不通。

So what is it that I'm doing wrong here? Incorrect configuration directive? Bad template or its location (I'm on OSX)? Should template be a git repo? A bare one?

那么我在这里做错了什么?配置指令不正确?错误的模板或其位置(我在 OSX 上)?模板应该是 git repo 吗?光秃秃的?

采纳答案by Tuxdude

The behavior you're seeing is the expected gitbehavior:

您看到的git行为是预期的行为:

If you read the manual correctly about the template directory:

如果您正确阅读了有关模板目录的手册:

TEMPLATE DIRECTORY

The template directory contains files and directories that will be copied to the $GIT_DIRafter it is created.

模板目录

模板目录包含创建后将复制到$GIT_DIR 的文件和目录。

The files which are copied from the template directory are placed in your GIT_DIRwhich defaults to the .gitdirectory under your repo's root directory.

从模板目录复制的文件放在你的GIT_DIR默认.git目录下你的repo根目录下。

git initdoes not support templates for the work-tree as far as I know. If this behavior is required, you should be able to get away with writing some simple bash aliases or functions to do this for you.

git init据我所知,不支持工作树的模板。如果需要这种行为,您应该能够避免编写一些简单的 bash 别名或函数来为您执行此操作。

回答by DylanYoung

You can do it, but it will require a few extra steps.

你可以做到,但它需要一些额外的步骤。

  1. Create your default directory structure as though it were a normal repo:

    mkdir template && cd template
    git init && touch README.md && cat ~/.gitignore_global > .gitignore
    git add --all && git commit -m "init"
    
  1. 创建您的默认目录结构,就像它是一个普通的存储库一样:

    mkdir template && cd template
    git init && touch README.md && cat ~/.gitignore_global > .gitignore
    git add --all && git commit -m "init"
    

(strictly speaking this last commit isn't necessary here, but you have to do them eventually, so why not now)

(严格来说,最后一次提交在这里不是必需的,但你最终必须这样做,所以为什么不现在呢)

  1. Now remove your working tree and move .git files up:

    mv .git/* ./ && rm -r README.md .gitignore .git
    
  2. You may now set this as your default template, but for the sake of example:

    mkdir ../myrepo && cd ../myrepo
    git init --template=../template
    

    (Note the interesting message: Reinitialized existing Git repository...)

  3. Now the important step:(your repo is up to date, but your working tree is not):

    git reset --hard
    

    (if you skip the commit earlier, you will have to commit here before resetting)

  1. 现在删除您的工作树并将 .git 文件向上移动:

    mv .git/* ./ && rm -r README.md .gitignore .git
    
  2. 您现在可以将其设置为默认模板,但为了举例:

    mkdir ../myrepo && cd ../myrepo
    git init --template=../template
    

    (注有趣的消息:Reinitialized existing Git repository...

  3. 现在重要的一步:(你的仓库是最新的,但你的工作树不是):

    git reset --hard
    

    (如果您之前跳过提交,则必须在重置之前在此处提交)

In the future, assuming you've set your default template, you simply

将来,假设您已经设置了默认模板,您只需

git init && git reset --hard

(I have no direct references, but this chaptersure helps.)

(我没有直接的参考资料,但本章确实有帮助。)

回答by Nichaladas

You could create a template repository which you clone each time you want to create a new project. You then delete the .git folder and copy the contents into your new project before calling git init. Assuming your template repository is called project_template:

您可以创建一个模板存储库,每次要创建新项目时都会克隆该存储库。然后在调用 git init 之前删除 .git 文件夹并将内容复制到新项目中。假设您的模板存储库名为 project_template:

$ git clone project_template new_project
$ cd new_project
$ rm -rf .git
$ git init

Not an ideal solution, but it can be scripted.

不是理想的解决方案,但可以编写脚本。

This is a modification of the steps provided here, which is specific to using GitHub.

这是对此处提供的步骤的修改,该步骤特定于使用 GitHub。

回答by David Kirkby

You might find the cookiecutter tool useful for automating the initial creation of a skeleton project based on a flexible template:

您可能会发现 cookiecutter 工具对于自动化基于灵活模板的骨架项目的初始创建很有用:

https://github.com/audreyr/cookiecutter

https://github.com/audreyr/cookiecutter

It is cross platform and actively maintained. Although it is implemented in python, it works well for any type of project and does not require any python expertise. Best of all, you can easily share your templates via github.

它是跨平台的并积极维护。尽管它是用 Python 实现的,但它适用于任何类型的项目,并且不需要任何 Python 专业知识。最重要的是,您可以通过 github 轻松共享您的模板。

回答by Pat-Laugh

A simple solution that works for me:

一个对我有用的简单解决方案:

  • Make a directory at a fixed location with what you need inside.
  • Then make a script that copies its contents to where you want to initiate a git repository.
  • 在固定位置创建一个目录,其中包含您需要的内容。
  • 然后制作一个脚本,将其内容复制到您想要启动 git 存储库的位置。

An example follows.

下面是一个例子。

By the way, some common .gitignore configurations are available here: https://gist.github.com/octocat/9257657

顺便说一下,这里提供了一些常见的 .gitignore 配置:https: //gist.github.com/octocat/9257657

Set up the directory:

设置目录:

mkdir ~/.git_template_dir
cd ~/.git_template_dir
echo "..." > .gitignore

Write the script:

编写脚本:

*nix:

*尼克斯:

#!/bin/bash
git init
cp -R ~/.git_template_dir/ .

Windows (%userprofile% equivalent to ~; %cd% to .) :

Windows(%userprofile% 相当于 ~;%cd% 到 .):

git init
xcopy %userprofile%\.git_template_dir\ %cd%

Then add to Path for easy access.

然后添加到 Path 以便于访问。