Smartly Managing Different Git Profiles

Photo by Firdouss Ross

While fixing the author and email properties for a bunch of existing commits on different repositories, I realised I often forget configuring the good information for both my professional & personal GIT repositories. I particularly skip to specify the good email address of the good GPG signature after checking them out.

I therefore looked around for an industrialised (~lazy) solution for fixing this issue once for all. I found a solution which fits my needs: the includeIf instruction. Among other things, this functionality helps me centralise my Git configuration and apply the good parameters/instructions (e.g., user.email) dynamically in every repository.

You could find below how I did that:

Having Git >=2.36 installed.

In case you use the current Ubuntu LTS or Linux Mint release, you can install the latest version of GIT by adding this repository into your APT sources:

You can find below the commands to run

1
2
sudo add-apt-repository ppa:git-core/ppa -y
sudo apt update && sudo apt full-upgrade -y

After that, you can check the version of GIT running this command:

1
2
git --version
git version 2.43.2

In this article, we will assume to have two different SCMs with two different SSH URLs:

  • The corporate GIT SCM: git@gitlab.INTERNAL_URL
  • Github.com: git@github.com

By the way, you can also use HTTPS URls.

Now let’s configure our main git configuration (e.g. $HOME/.gitconfig)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[init]
	defaultBranch = main
[pull]
	rebase = merges
[core]
	autocrlf = true

[includeIf "hasconfig:remote.*.url:git@gitlab.INTERNAL_URL:*/**"]
    path = ~/.gitconfig-corporate
[includeIf "hasconfig:remote.*.url:git@github.com:*/**"]
	path = ~/.gitconfig-github

As mentioned earlier, I use in this configuration the includeIf functionality for checking the remote URL. Regarding the remote URL (it also works with HTTPS URLs), either the corporate or GitHub configuration will be applied on the fly while applying git commands in my repos.

I then configured my corporate git configuration as following:

1
2
3
4
5
6
[user]
	name = Alexandre Touret
	email = alexandre.touret@XXX.com
[credential]
	username = XXXX
	helper = cache

and the GitHub configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[user]
  	email = alexandre-touret@users.noreply.github.com
	signingkey = XXXXXXXXX
	name = Alexandre Touret
[credential]
	username = XXXX
	helper = cache
[gpg]
	program = gpg
[commit]
	gpgsign = true

Hopefully, GitHub has enabled SSH connections through the standard HTTPS port. You can then set up your SSH client adding this configuration in the $HOME/.ssh/config:

1
2
3
4
5
Host github.com
    Hostname ssh.github.com
    Port 443
    User git
    IdentityFile ~/.ssh/id_rsa_github

If you want to knwo more about GitHub SSH configuration, check out the documentation

In one Git repository, you can check this configuration typing the command:

Here an example for a GitHub repository

1
2
> git config --get user.email
alexandre-touret@users.noreply.github.com

In this short article I aimed to explain how to smoothly handle two (or more) Git profiles in the same development environment. I chose to use the remote URL to segregate each one. This configuration can finally help you apply automatically the good Git information while committing your work without setting up each project individually.

Hope this helps!