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:
git init --template=<template_directory>
, but it's bothersome.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?
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.
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.
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.
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.
You can do it, but it will require a few extra steps.
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)
Now remove your working tree and move .git files up:
mv .git/* ./ && rm -r README.md .gitignore .git
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...
)
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With