DEPRECATED: Switch git user based on the name of the directory.
as @crea1
said in stackoverflow:
As of git version 2.13, git supports conditional configuration includes. In this example we clone Company A's repos in ~/company_a directory, and Company B's repos in ~/company_b.
In your .gitconfig
you can put something like this.
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
Example contents of .gitconfig-company_a
[user]
name = John Smith
email = [email protected]
Example contents of .gitconfig-company_b
[user]
name = John Smith
email = [email protected]
TEMPLATE DIRECTORY Files and directories in the template directory whose name do not start with a dot will be copied to the $GIT_DIR(default: .git) after it is created.
The files which are copied from the template directory are placed in your GIT_DIR which defaults to the .git directory under your repo's root directory.
so we can put hooks in the template dir, and it will be copied to the newly created projects.
mkdir -p ~/.config/git/template/hooks
git config --global init.templateDir ~/.config/git/template
first of all, pls read: githooks Documentation
cp -i pre-commit.py ~/.config/git/template/hooks/pre-commit
then next time you create a git project, the hook will be active.
the key has
means $(pwd)
has $(value)
, for example, ~/0Workspace/multi-user-git-hook
has 0Workspace
.
[multi "ant"]
has=0Ant
user-email[email protected]
user-name=xx
user-signingkey = xxxx
[multi "gmail"]
has=0Workspace
user-email = [email protected]
user-name = xxxxxx
user-signingkey = xxxxx
set this in your ~/.gitconfig
use dash(-
) to separate the different parts, for example: user.email
-> user-email
.
because .gitconfig
can not use dash as the key.
you can use make install
to place the pre-commit
file, and change the .gitconfig
mannualy.
Thanks to pgils/multi-email-git-hook, the project has helped me a lot.