I can set my email via:
git config --global user.email tgkprog@xyz.dom
this is dangerous when I use this my office or in a multi repository
provider environment where my email is different for a few (like
tgkprog@search.com for github and tushar@mycompany.com for my company
private repo). I know I can over ride it per repository, but sometimes
forget to do that. And even if I unset it, it inadvertantly gets set
elsewhere when I make a repo and the site 'helps' me by showing me the
commands to init and clone my new repo.
I did an analysis on a bunch of company git repositories using jgit
(only master branch), and we have 57 emails out of 346 which are not
the company email. Also in there are cases when name is the same but
some commits are by email 1 and others by email 2, because of this
global config. As some of us work on open source and company repos on
the same computer.
Feature request : can we have a config for email per repo domain ?
Something like:
git config --global domain.user.email tgkprog@test.xyz.com
testing.abc.doman:8080
git config --global domain.user.email tgkprog@xyz.com abc.doman:80
git config --global domain.user.email tgkprog@search.com github.com
So when remote URL has github.com push as tgkprog@search.com but for
testing.abc.doman:8080 use tgkprog@test.xyz.com ?
For me one name is enough. But can do the same for name if others need it?
Thank you.
Regards
Tushar Kapila
Hey Tushar,
When you run `git config --global user.email abc@xyz.com` it writes
out this setting in ~/.gitconfig which is then considered to be
"global" ie. this same email will be used for every repo, but ...
You can always override this. Let's say there is a repo named "foo" in
which you want the email "xyz@abc.com", then you go inside that folder
and type `git config user.email xyz@abc.com`. Note: the option
`--global` is skipped here. This creates a file `.gitconfig` in the
folder "foo" and now whenever you commit, the .gitconfig file inside
the repo will get the first preference and then the global
~/.gitconfig.
This will work for you assuming that you have different repos for your
company and for your open source work. Will this solve your problem?
Regards,
Pranit Bauva
From: Andrew Ardill <hidden> Date: 2017-02-22 23:23:30
On 23 February 2017 at 00:12, Tushar Kapila [off-list ref] wrote:
I can set my email via:
git config --global user.email tgkprog@xyz.dom
this is dangerous when I use this my office or in a multi repository
provider environment where my email is different for a few (like
tgkprog@search.com for github and tushar@mycompany.com for my company
private repo).
There has been a large discussion around this idea before, see this
thread for example [0].
The proposed idea was to have something set in your global config that
would only be turned on if you were within a given directory. This
would allow you to have two root directories, one for your work
projects and one for open source projects (for example) and any git
repositories within those folders would each would have their own
config options automatically applied based on their location.
There was a patch suggested, and it worked quite well, but nothing
further has been done to my knowledge.
[0] http://public-inbox.org/git/7vvc3d1o01.fsf@alter.siamese.dyndns.org/
Regards,
Andrew Ardill
From: Jeff King <hidden> Date: 2017-02-23 00:49:30
On Wed, Feb 22, 2017 at 06:42:02PM +0530, Tushar Kapila wrote:
Feature request : can we have a config for email per repo domain ?
Something like:
git config --global domain.user.email tgkprog@test.xyz.com
testing.abc.doman:8080
git config --global domain.user.email tgkprog@xyz.com abc.doman:80
git config --global domain.user.email tgkprog@search.com github.com
So when remote URL has github.com push as tgkprog@search.com but for
testing.abc.doman:8080 use tgkprog@test.xyz.com ?
The idea of "the domain for this repo" doesn't really match Git's
distributed model. A repo may have no remotes at all, or multiple
remotes with different domains (or even remotes which do not have a
domain associated with them, like local files, or remote helpers which
obscure the domain name).
It sounds like you're assuming that the domain would come from looking
at remote.origin.url. Which I agree would work in the common case of
"git clone https://example.com", but I'm not comfortable with the number
of corner cases the feature has.
I'd much rather see something based on the working tree path, like Duy's
conditional config includes. Then you write something in your
~/.gitconfig like:
[include "gitdir:/home/peff/work/"]
path = .gitconfig-work
[include "gitdir:/home/peff/play/"]
path = .gitconfig-play
and set user.email as appropriate in each.
You may also set user.useConfigOnly in your ~/.gitconfig. Then you'd
have to set user.email in each individual repository, but at least Git
would complain and remind you when you forget to do so, rather than
silently using the wrong email.
-Peff
From: Igor Djordjevic BugA <hidden> Date: 2017-02-23 01:30:30
Hi Tushar,
On 22/02/2017 14:12, Tushar Kapila [off-list ref] wrote:
So when remote URL has github.com push as tgkprog@search.com but for
testing.abc.doman:8080 use tgkprog@test.xyz.com ?
I`m not sure if this is sensible, as authorship information is baked
into the commit at the time of committing, which (usually ;) happens
before you get to 'git push' to the other repo.
If possible, changing this info after the fact, on 'git push', would
influence the existing commit you`re trying to send over, so your
'git-push' would have a surprising consequence of not actually
pushing your desired commit at all, but creating a totally new commit
inside the other repo -- this new commit would be exactly the same
patch-wise (in regards to differences introduced), but because of the
changed user info it would be considered a different commit
nonetheless (different hash).
... I know I can over ride it per repository, but sometimes
forget to do that. And even if I unset it, it inadvertantly gets set
elsewhere when I make a repo and the site 'helps' me by showing me the
commands to init and clone my new repo.
Otherwise, as you already stated that you find the current local (per
repo) user settings override logic inconvenient (error-prone), you
might be interested in approach described in this[1] Stack Overflow
post.
In short, it uses a template-injected 'post-checkout' hook (triggered
on 'git clone' as well) alongside '.gitconfig' (global) settings to
achieve what seems to be pretty similar to what you asked for (but
might be a bit more sensible), where you may fine-tune it further to
better suit your needs.
On 20/02/2017 21:12, Grant Humphries[2] wrote[1]:
This answer is partially inspired by the post by @Saucier, but I was
looking for an automated way to set user.name and user.email on a per
repo basis, based on the remote, that was a little more light weight
than the git-passport package that he developed. Also h/t to @John
for the useConfigOnly setting. Here is my solution:
.gitconfig changes:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
post-checkout hook which should be saved to the following path:
~/.git-templates/hooks/post-checkout:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
I use different credentials for github and gitlab, but those
references in the code above could be replaced or augmented with any
service that you use. In order to have the post-checkout hook
automatically set the user name and email locally for a repo after a
checkout make sure the service name appears in the remote url, add it
to the services array in the post-checkout script and create a
section for it in your .gitconfig that contains your user name and
email for that service.
If none of the service names appear in the remote url or the repo
doesn't have a remote the user name and email will not be set
locally. In these cases the user.useConfigOnly setting will be in
play which will not allow you to make commits until the user name and
email are set at the repo level, and will prompt the user to
configure that information.
Regards,
Buga
*P.S.* For the purpose of completeness and archiving I copied the
Stack Overflow post[1] here as well, but all the credits go to its
author[2] (you may upvote the linked post[1] if you find it helpful).
Please feel free let me know if this practice is otherwise to be
avoided.
[1] http://stackoverflow.com/a/42354282
[2] http://stackoverflow.com/users/2167004
From: Igor Djordjevic <hidden> Date: 2017-02-23 19:29:37
Forwarding a message that ended on my end only, probably by accident.
-------- Forwarded Message --------
Subject: Re: feature request: user email config per domain
Date: Thu, 23 Feb 2017 13:32:56 +0530
From: Tushar Kapila <redacted>
To: Igor Djordjevic BugA <redacted>
Hello All,
I'd much rather see something based on the working tree path, like
Duy's conditional config includes. Then you write something in your
~/.gitconfig
This would allow you to have two root directories, one for your work
projects and one for open source projects (for example).
I guess this can be extended for any number of root directories. Like,
when a consultant has multiple employer email ids.
This sounds great and it would enforce at commit time, which as
pointed out, is the correct time to do it. If for some reason it is
not adopted I at least hope that we have a simple global config which
specifies that user must set email for each repo and to ignore any
global config.
Am sure this can be done via hooks, but I would like something that is
really simple for newbies and companies to enforce with minimal
instruction.
* Will try what Igor and Grant Humphries suggests.
* Thank you all for your replies, and a big thank you for git - its a
great tool. I used to dream of something like it when I was stuck with
svn (pre 2010 I was introduced to git late.)
[1] http://stackoverflow.com/a/42354282
[2] http://stackoverflow.com/users/2167004