Ryan C. Thompson
Ryan C. Thompson

Reputation: 42060

Automatically commit an empty .gitignore file after git init?

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?

Upvotes: 10

Views: 4288

Answers (3)

Chris Salij
Chris Salij

Reputation: 3126

It's not pretty, but it'll work. Whenever you want to inititalize a repo, run the following two commands.

git init
touch .gitignore

Then to make sure git is tracking your gitignore file run:

git add .gitignore
git commit .gitignore -m "Adding .gitignore"

Depending on how you set up your repo, you may be doing the second two commands anyway.

Upvotes: 3

Cascabel
Cascabel

Reputation: 497262

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.

Upvotes: 14

Greg Hewgill
Greg Hewgill

Reputation: 993901

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.

Upvotes: 3

Related Questions