7

I work with multiple Git remote repositories and each need different Git credentials (Name and Mail).

Is there any solution such as a script or best practices how to manage them?

I know about "config --local", but I don't want to set this variables manually everytime.

MicTech
  • 42,457
  • 14
  • 62
  • 79
  • See also [Can I specify multiple users for myself in .gitconfig?](http://stackoverflow.com/questions/4220416/can-i-specify-multiple-users-for-myself-in-gitconfig) –  May 19 '14 at 01:46

2 Answers2

11

It looks like just saying git config user.name (or user.email) in a particular repository, without specifying --global or --system would do the trick. The default is to set the configuration in the current repository, and you have to give it explicit options for it to write to your user-level or system-wide configuration instead.

I don't know how one would do this if you're freshly cloning repositories that need different configuration, though. Perhaps you could write a small script that wraps git clone to clone some repository, and then set the appropriate configuration based on whatever information? If you drop the script in /usr/lib/git-core named something like git-nclone, you can then run it as git nclone.

Edit: Since you don't want to manually set it every time, how about a clone wrapper that remembers the various sets you actually use, and lets you pick the appropriate one for the repository you're cloning. It could even have smart defaults based on where you're clone is coming from.

Phil Miller
  • 36,389
  • 13
  • 67
  • 90
  • 1
    changes to the local config appear in the .git/config file – Abizern Jan 08 '11 at 19:31
  • 4
    If you have a large number of different configs, I'd suggest leaving your global config (~/.gitconfig) without a name/email setting, so that if you try to commit in a repository which hasn't been configured, you'll get a warning. If on the other hand you have two primary configs, set the "safe" one globally, then override it in individual repos. – Cascabel Jan 08 '11 at 21:32
  • 1
    Git should check the entirety of `$PATH` for executable files named `git-foo`, actually, so dropping it in `~/bin/` and making sure it's in your `$PATH` should work fine. – fow Jan 10 '11 at 01:23
2

I created a couple of aliases that looks like this:

The idea being I can save my credentials (email, username) in the alias definition. Then when I want to clone or initialise, I don't have to perform a git config every time.

when initialising:

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:

when initialising:

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 execute both the normal clone operation and the config operations. For now it requires that you provide the path to the folder you are cloning to in order to configure your credentials properly.

Community
  • 1
  • 1
Desmond Nzuza
  • 130
  • 1
  • 11