LightCC LightCC - 3 years ago 181
Git Question

Can I use different author info for Git repo, depending on URL

At work, we have Github Enterprise, but sometimes I also pull from the public Github.com. I need to use my work author info for any Github Enterprise activity, but I don't want to confuse my work credentials with my personal credentials when updating Github.com repos that I already have activity on.




As a comparison, I was able to successfully setup different proxy information based on URL as follows:

git config --global http.https://github.com.proxy http://<myworkproxy:port>
git config --global https.https://github.com.proxy http://<myworkproxy:port>


which will use the proxy only when
https://github.com
is in the remote URL.

This can be verified by entering a real remote URL as follows, and Git will spit out the proxy to be used:

git config --url-match https.proxy https://github.com/<rest of repo URL>





So, I tried the above for
user.name
and
user.email
as well:

git config --global user.https://github.com.name <My Github.com user name>
git config --global user.https://github.com.email <My Github.com user email>


This checking my global config file (use
git config --global -e
to pull it up in an editor), everything appears to be setup correctly:


[user]
name = <My work user name>
email = <My work user email>
[user "https://github.com"]
name = <My github.com user name>
email = <My github.com user email>



Even if I use
git config --url-match user.name https://github.com/<rest of repo URL>
it will show me the github.com user name rather than the work user name, so the filtering is working correctly.

However, when I commit with git (whether from a GUI, or directly on the command line), it still always uses my work
user.name
and
user.email
.

Is it not possible to URL match the user name and email globally? Or am I doing something wrong here?

I can update the name and email for every repo directly if needed, but I was hoping for a more global solution.

Thanks!

Answer Source

git doesn't allow you to use a URL match to set the user.name and user.email settings.

URL matching is generally limited to those settings which involve interacting with remote servers, such as the http.* and credential.* settings. If you run git config --help, you can see which settings allow this, because they are documented with the <url> component.

The reason for this is that git doesn't consider the clone or push URLs when setting up non-remote settings.

You can, however, set these on a per-repository basis. If you want to do this automatically, you can set them when you clone by setting up a template directory with an appropriate config file (see git init --help) for each user and using the --template option when you clone. This can be pushed into an alias if you like.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download