I’m sure you’ve never had to do a search for “fix author git multiple commits”. Right? Well, I’m not going to talk about how to do that fix (ugh it’s a pain…). What I DO have to share is a way to hopefully avoid that mess in the future without any crazy arcane incantations or embracing tedious per-repository authorship edits.
Global Config
A little background – Git supports multiple layers of configuration at system, global, repository, and of course, CLI levels. The nearest config entry wins (system takes lowest priority with CLI highest).
It’s pretty common to simply run “git config –global user.email “[email protected]” and call it a day.
This command updates your global SSH config (located at ~/.gitconfig
on Linux) with your preferred
email address.
That works if you always use the same email regardless of repository. But if you prefer to keep (for example) personal projects separate from open source contributions, this gets to be a pain as you need to override at least the email config entry per repository.
Override Config
Just like my trick to use
a different SSH key for some repositories, we can use built-in git config directives to make this
work. Specifically, includeIf
permits
including config files based on some conditional.
I always check my repositories out under ~/code
, and further group related repos together (eg,
~/code/work/{this,that,another}_repo
) in subdirectories. It’s pretty simple to check if the
current repo is in a specific subdirectory using gitdir
. So I just add a .gitconfig
to the
subdirectory and in my global config add a conditional include for that subdirectory. See below for
an example!
One additional trick that I use in tandem with my SSH configuration is to rewrite the git remote
URL. This allows me to clone exactly as I normally would, but rewrite the URL with something
specific to that project. From there my SSH config handles rewriting it back when actually
interacting with the git remote.
Turns out I was missing a really simple way to avoid this URL hack: the
core.sshCommand
config entry. This is a ton simpler and doesn’t confuse other tools.
Example
Here are my config files, lightly edited:
User configuration
[user]
name = "Jesse Roberts"
email = "*****@**********.***"
[core]
editor = vim
[init]
defaultBranch = main
[rebase]
autoStash = true
[push]
default = upstream
[includeIf "gitdir:~/code/work/"]
path = "~/code/work/.gitconfig"
Subdirectory configuration overrides
[user]
email = [email protected]
name = "Jesse Roberts"
[core]
sshCommand = ssh -i ~/.ssh/<my-work-ssh-key>