Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically add .gitignore and hooks on git init

Tags:

git

Is there a way to tell git to automatically create/populate .gitignore and certain files in the .git/hooks folder every time git init is run on a certain machine? Maybe a global config somewhere?

We have symlinks that need to be ignored across the board, as well as pre-receive and post-receive hooks that need to be set up for every repo, so this would be easier than doing it manually for each one.

Thanks.

like image 567
jraede Avatar asked May 20 '13 20:05

jraede


People also ask

Does git init Add Gitignore?

gitignore contents you want in a file you name exclude in the folder /path/to/template/info . Then it will effectively be a . gitignore file in all new repositories created by git init .

Is Gitignore created automatically?

gitignore files are automatically generated to exclude the generated source files. We would like to have these source files under version control. The major reason being, that not all developers have XCore installed in there Eclipse, but still need to compile the sources.

Should .gitignore be added to repository?

Normally yes, . gitignore is useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOG or something. In those cases you probably don't want to force that on anyone else.


1 Answers

You can achieve this using a git template directory

git config --global init.templatedir /path/to/template 

You can then add files to the folder /path/to/template/hooks and they will be automatically copied to the .git/hooks folder on git init

You can place the .gitignore contents you want in a file you name exclude in the folder /path/to/template/info. Then it will effectively be a .gitignore file in all new repositories created by git init.

Note that the .gitignore file is not populated with the content of exclude. On git init the exclude file in the infofolder will be copied into the .git/info folder of your git repository. This will cause the file patterns listed in exclude to be ignored, just like patterns in .gitignore.

If you are on unix, there is even a default template directory /usr/share/git-core/templates. On MacOS the template directory is in /usr/local/share/git-core/templates

like image 122
Klas Mellbourn Avatar answered Sep 19 '22 08:09

Klas Mellbourn