Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up a default directory structure on git init

Tags:

git

templates

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

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

  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.templatedir configuration variable. Now we're talking!

So I sudo mkdir /usr/share/git-core/templates/my_template and touch some files in it, then I vim ~/.gitconfig and append:

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

And git config -l tells me:

...
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_template doesn't work either.

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?

like image 535
Oleg Avatar asked May 03 '13 16:05

Oleg


People also ask

How do I initialize a directory in git?

git init Existing Folder For an existing project to become a Git repository, navigate into the targeted root directory. Then, run git init . Or, you can create a new repository in a directory in your current path. Use git init <directory> and specify which directory to turn into a Git repository.

What is template directory in git?

The template directory contains files and directories that will be copied to the $GIT_DIR after it is created. The files which are copied from the template directory are placed in your GIT_DIR which defaults to the . git directory under your repo's root directory.

Where is the default git directory?

The default location that Git Bash starts in is typically the home directory (~) or /c/users/<Windows-user-account>/ on Windows OS. To determine the current directory, type pwd at the $ prompt.


2 Answers

The behavior you're seeing is the expected git behavior:

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_DIR after it is created.

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

git init does 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.

like image 130
Tuxdude Avatar answered Sep 18 '22 04:09

Tuxdude


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" 

(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)

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

git init && git reset --hard 

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

like image 42
DylanYoung Avatar answered Sep 20 '22 04:09

DylanYoung