9

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?

Community
  • 1
  • 1
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159

3 Answers3

14

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • Should I prefix that with `git status &>/dev/null ||` or something, so that the `myinit` command will be a no-op if a git repo already exists in the current directory? – Ryan C. Thompson Mar 30 '11 at 18:44
  • @Ryan: You can do something like that if you want, but it doesn't matter much - `git init` in an existing repo recopies the template (particularly, the `*.sample` hooks) and creates the directory structure (which will be effectively no-op). If you do prefix it, `git status` isn't what you want - among other things, it's slow. I'd recommend `git rev-parse HEAD &> /dev/null`. That will be successful only within a repo where `HEAD` points to a commit - i.e. if you've already run `git init` it will fail, causing the rest of your steps to happen. – Cascabel Mar 30 '11 at 20:19
  • Ooh, can you post that [here](http://stackoverflow.com/questions/5491832/how-can-i-check-whether-a-git-repository-has-any-commits-in-it) so I can accept it as the answer? – Ryan C. Thompson Mar 30 '11 at 20:44
  • Just as a note, using command-line Git on Windows seems to require single quotes instead of double for the commit description. – davidcesarino Jul 03 '12 at 01:55
3

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Yeah, I figured that what git refers to as hooks were not the right way to approach this. That's why I said something *like* a hook -- I just meant any way to trigger custom behavior after running `git init`. – Ryan C. Thompson Mar 30 '11 at 18:45
3

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.

Chris Salij
  • 3,096
  • 5
  • 26
  • 43