36

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.

jraede
  • 6,846
  • 5
  • 29
  • 32

3 Answers3

43

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

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • 1
    I will also add that *all* files in that `/path/to/template` will be added to the `.git` folder in your project. So make sure that the `.gitignore` template is in a folder by itself. – micnguyen Jul 02 '19 at 04:38
  • 1
    @micnguyen - could you please explain your sentence? It does not make sense to me. – Martin Vegter Aug 03 '19 at 05:20
  • @Klas Mellbourn - the `.gitignore` part does not work. I have added lines to `/usr/share/git-core/templates/info/exclude`, but when I create new repository, no `.gitignore` file is populated from that template. I have `git version 2.20.1` – Martin Vegter Aug 03 '19 at 06:36
  • @MartinVegter I've tried to clarify my answer, see the paragraph beginning with `Note that` – Klas Mellbourn Aug 03 '19 at 07:03
  • For future users: if you want to legitimately add a `.gitignore` to the repository on init, [my answer](https://stackoverflow.com/a/67409111/11659881) to a different question outlines how you'd do this for `.gitattributes` which is identical to how you'd do it for `.gitignore`. The only caveat is you'll need to create an alias like `git create` which runs both `git init` and populates the ignore file. – Kraigolas May 05 '21 at 21:48
3

.gitignore_global in your home directory. If the file isn't there create it. Same syntax as .gitignore files. Be careful what you place in this file!

If all users wish to share the same .gitignore file, you can create one in

/.SHARED_GIT_IGNORE  

Then create soft links to it in each respective users home directory.

/Users/ALL_USERS/.gitignore_global -> /.SHARED_GIT_IGNORE
MobA11y
  • 18,425
  • 3
  • 49
  • 76
  • So if we have 10 developers, we need a .gitignore_global file in each of their home directories? There has to be a way to do a truly global .gitignore... – jraede May 20 '13 at 20:48
  • 1
    There are alternatives, but I mean, is it hard to set up 10 times? What if each developer wants to ignore different things? Why would you have 10 developers sharing a machine? Alternatively, an overly global git ignore just sounds very dangerous. If you must you could all symlink to the same .gitignore_global somewhere else in the file system, so you can keep up the same ignored file, without all having to make changes to it... – MobA11y May 20 '13 at 21:05
  • I was thinking it could be annoying if we have to make a change, but you're right, it's only 10 files. – jraede May 20 '13 at 21:26
1

If we are talking about a repo containing JavaScript and using npm, then you can use husky to set up commit hooks.

npm install husky --save-dev

// package.json
{
  "husky": {
    "hooks": {
      "pre-commit": "npm test",
      "pre-push": "npm test",
      "...": "..."
    }
  }
}

This does not answer the original question directly, but may be relevant to many developers.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158