How to manage multiple user configurations in Git?

I work with several remote Git repositories, and each one needs a different Git credential (name and mail).

Is there any solution like a script or best practices for managing them?

I know about "config -local", but I do not want to set these variables manually each time.

+6
git
source share
2 answers

It seems like just saying git config user.name (or user.email ) in a specific repository without specifying --global or --system will do the trick. By default, the configuration setting in the current repository is used, and you must give it explicit options in order to write to your user or system-wide configuration instead.

I do not know how to do this if you recently clone repositories that need a different configuration. Perhaps you could write a small script that wraps git clone to clone some repository, and then set the appropriate configuration based on any information? If you release the script in /usr/lib/git-core , named something like git-nclone , you can run it as git nclone .

Change Since you don’t want to manually install it every time, what about the clone shell, which remembers the different sets that you use and allows you to choose the one suitable for the repository that you are cloning. It can even have smart defaults based on where you clone from.

+10
source share

I created a couple of aliases that look like this:

The idea is that I can save my credentials (email address, username) in the alias definition. Then, when I want to clone or initialize, I do not need to execute git config every time.

upon initialization:

 initgithub = !git init && git config user.email [ youremailfor@github.com ] && git config user.name [yourgithubusername] initbitbucket = !git init && git config user.email [ youremailfor@bitbucket.com ] && git config user.name [yourbitbucketusername] 

when cloning:

 clonegithub = "!f() { git clone $1 $2; cd $2; git config user.email [ youremailfor@github.com ]; git config user.name [yourgithubusername]; }; f" clonebitbucket = "!f() { git clone $1 $2; cd $2; git config user.email [ youremailfor@bitbucket.com ]; git config user.name [yourbitbucketusername]; }; f" 

ussage:

upon initialization:

git initgithub

git initbitbucket

when cloning:

git clonegithub https://github.com/pathtoproject.git / c / temp / somefolder / project

git clonebitbucket https://github.com/pathtoproject.git / c / temp / somefolder / project

When cloning, you can basically create a function that will perform both the normal cloning operation and the configuration operation. At the moment, this requires you to specify the path to the folder you are cloning in order to properly configure your credentials.

0
source share

All Articles