Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically commit an empty .gitignore file after git init?

Tags:

git

I would like to start off all my git repositories with an "empty" initial commit by simply touching an empty .gitignore file and committing it. My reasons are roughly the same as in this question. Baiscally, the first commit (the "tail") has no parent commit, so various weird and unexpected things happen that would not happen with other commits.

So is there something like a post-init hook that I can use to follow every git init with touch .gitignore && git commit .gitignore -m "Initial commit"? Or should I write my own custom init command that does this?

like image 377
Ryan C. Thompson Avatar asked Mar 29 '11 22:03

Ryan C. Thompson


2 Answers

If you want a commit in the repo, don't use git init - it makes empty repos! Use git clone instead. Just make one repository with a commit of your empty gitignore, and clone that whenever you want this particular pseudo-empty repository. (You could host it publicly on github if you want it available anywhere.)

Alternatively, on your local machine, make a git alias for what you want:

[alias]
    myinit = !git init && touch .gitignore && git add .gitignore && git commit -m "empty gitignore"

Note that you can't make aliases with the same name as built-in commands.

like image 96
Cascabel Avatar answered Oct 16 '22 00:10

Cascabel


When you do a git init, the .git directory (which contains the hooks) is initialised to default values. Therefore there are no hooks existing after a git init. A hypothetical "post-init" hook would be useless because the only time it could run would be at the point where the hooks directory is initialised (and doesn't contain any active hooks).

It sounds like you might be better off writing a custom command that does this, since a hook is not the appropriate solution.

like image 22
Greg Hewgill Avatar answered Oct 15 '22 23:10

Greg Hewgill