jraede
jraede

Reputation: 6896

Automatically add .gitignore and hooks on git init

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.

Upvotes: 36

Views: 22299

Answers (3)

Klas Mellbourn
Klas Mellbourn

Reputation: 44387

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

Upvotes: 44

Klas Mellbourn
Klas Mellbourn

Reputation: 44387

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.

Upvotes: 1

MobA11y
MobA11y

Reputation: 18900

.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

Upvotes: 3

Related Questions