Loading...
Loading...
Marco Franssen

Have you ever struggled to commit with the right email address across different repositories? It used to happen to me often, but for the past few years I've used a simple folder-based Git configuration to avoid that mistake. On my work machine, I also contribute to open source in my spare time—to build my skills and because I believe in it. During work, I sometimes contribute fixes to open-source projects we use, and I want those commits to use my work email address. Keeping track of the right address for every commit is a hassle. In this post, I'll share my simple solution.
I'll assume you're configuring this on your work machine. If you're setting it up on a personal machine, reverse the work and personal settings in my examples.
First, configure your Git user globally with your company email address. You can do that with the following commands.
git config --global user.name "Marco Franssen"
git config --global user.email "marco.franssen@my-company.org"With these settings, repositories you clone or create on this machine use this identity for commits unless you override it. So what about personal projects on the same machine?
For personal projects, I use a folder-based approach. Git's global configuration file, ~/.gitconfig, lets me apply different user information to projects in a specific folder. I keep all my code in ~/code, with personal projects in ~/code/priv. Here's an example of that layout.
$ tree -L 3 code
├── priv
│ ├── blog
│ ├── helm
│ ├── kubernetes
│ └── spire
├── spire
├── helm
├── work-project-a
├── work-project-b
├── work-project-c
└── work-project-d
9 directories, 0 filesIn my spare time, I work on repositories in the ~/code/priv/… folders. During work, I use projects outside ~/code/priv. This also lets me contribute to open-source projects with my company email address. The only downside is that some projects are cloned twice. I tried using symbolic links to avoid that, but unfortunately it didn't seem to work.
To use my personal email address for commits in ~/code/priv/…, I create another Git configuration file that I can conditionally include in the global configuration.
git config --file ~/.gitconfig-private user.email marco.franssen@my-private-email.nlThis will result in the following ~/.gitconfig-private file.
$ cat ~/.gitconfig-private
[user]
email = "marco.franssen@my-private-email.nl"You can put other overrides in this file too, such as a different GPG key for personal contributions.
Now here's where the magic happens. We'll add a conditional include to our global configuration. When we're working in a particular folder, Git will load ~/.gitconfig-private and use its settings to override the global ones.
We can do that with the following command.
git config --global includeIf."gitdir:~/code/priv/**/*".path "~/.gitconfig-private"This produces the following ~/.gitconfig file (omitting any other settings you may have).
$ cat ~/.gitconfig
[user]
name = "Marco Franssen"
email = "marco.franssen@my-company.org"
[includeIf "gitdir:~/code/priv/**/*"]
path = ~/.gitconfig-privateWith this global Git configuration, our commits use the work email address by default, except in repositories under ~/code/priv. On a personal machine, you might want the reverse: use your personal email globally and override it with your work email for repositories under ~/code/work.
Here's what the complete configuration looks like (omitting any other settings you may have).
$ cat ~/.gitconfig
[user]
name = "Marco Franssen"
email = "marco.franssen@my-company.org"
[includeIf "gitdir:~/code/priv/**/*"]
path = ~/.gitconfig-private
$ cat ~/.gitconfig-private
[user]
email = "marco.franssen@my-private-email.nl"For more information on conditional includes, see the Git documentation.
Marco Franssen
Explore a 2022 approach to storing, signing, and discovering package SBOMs and build provenance in an OCI registry with cosign, fatt, and sget.
Marco Franssen
Sign Docker images and attest SBOMs and build provenance with Sigstore in GitHub Actions, applying SLSA requirements to your release workflow.
Marco Franssen
Use Helmsman to manage Helm charts as code, organize Kubernetes environments, preview deployment plans, and apply releases in a GitOps workflow.
Marco Franssen
Deploy a highly available HashiCorp Vault cluster on AWS EKS with Helm, use Consul for storage, and configure AWS KMS for automatic unsealing.
Test reply to check my Bluesky comment integration on my Blog. See the Bluesky comments component at the bottom of my Post. What do you think about this integration?
0
0
0