From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: David Miguel Susano Pinto <carandraug@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: git init with specified user.email
Date: Thu, 8 Jan 2026 23:22:11 +0000 [thread overview]
Message-ID: <aWA8I82PXXVjiMVR@fruit.crustytoothpaste.net> (raw)
In-Reply-To: <875x9cvx44.fsf@word>
[-- Attachment #1: Type: text/plain, Size: 3285 bytes --]
On 2026-01-08 at 17:21:31, David Miguel Susano Pinto wrote:
> git checkout has a --config option so one can do:
>
> git clone --config 'user.email=email-for-this-clone' ...
>
> which I find nice to setup as git alias:
>
> clone-work = clone --config 'user.email=my-work-email'
>
> (while leaving the default clone for my personal projects; or an alias
> for clone-personal with the work email as the clone default).
The reason this functionality exists is because it sets those
configuration variables as soon as the repository is initialized and
_before_ actually downloading the data. As an example, if you wanted to
set a split index or a default line ending configuration, you'd want to
set that before the index was created and the working tree was checked
out, so you can't do that after the clone command completes.
> I would like something similar for git init. However, git init does not
> have a `--config` option. I tried to use
>
> git -c 'user.email=my-work-email' init
>
> but that's not working (I'm guessing it only picks up configurations
> mentioned on the man page for git-init).
`git -c` (where `-c` precedes the subcommand) is different. It sets the
configuration option _for this command only_ and doesn't save it in the
config. For instance, if you want to run a single diff command without
colours, you can run `git -c color.diff=false diff`; that won't affect
any of the configuration files.
The reason that Git doesn't provide a config option for `git init` is
because, except for things like the hash algorithm and ref format,
almost nothing is done to the repository when the command completes.
There isn't even an index created, so setting configuration like split
index in a separate configuration command isn't a problem.
> I am currently working around this with a template for work but would
> prefer to init with `-c` (or similar) or at least understand why this is
> not possible.
This is possible with an alias like so:
init-work = "!f() { dir=\"$1\"; shift; git init \"$@\" \"$dir\"; git -C \"$dir\" config set user.email my-work-email; }"
This would work slightly differently in that you'd need to specify the
directory first unless you wanted to adjust the alias to do more
complicated shell parsing. You could also implement a shell script
called `git-init-work` (using `git rev-parse --parseopt`[0] or otherwise)
that would handle the complexities and it would work just like the
alias as long as you put it in `PATH`.
One other approach that might work for you is the includeIf
functionality in the config file. You could do something like this in
your main config file:
[includeIf "gitdir:~/checkouts/work/**"]
path = ~/.config/git/config-work
And then set the `user.email` value in `~/.config/git/work`. That would
mean that any repositories under `~/checkouts/work` would automatically
have the config values in `~/.config/git/config-work` set.
[0] If you want an example of using `git rev-parse --parseopt`, you can
see https://github.com/bk2204/dotfiles/blob/dev/bin/dct-mtree, which
demonstrates some of the functionality. The manual page is also
helpful.
--
brian m. carlson (they/them)
Toronto, Ontario, CA
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]
prev parent reply other threads:[~2026-01-08 23:22 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-08 17:21 git init with specified user.email David Miguel Susano Pinto
2026-01-08 23:22 ` brian m. carlson [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aWA8I82PXXVjiMVR@fruit.crustytoothpaste.net \
--to=sandals@crustytoothpaste.net \
--cc=carandraug@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox