Smartly Managing Different Git Profiles
1 Introduction
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:
2 Prerequisites
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
|
|
After that, you can check the version of GIT running this command:
|
|
3 The configuration
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
)
|
|
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:
|
|
and the GitHub configuration
|
|
4 Gentle reminder: how to use GitHub SSH connection without using Standard 22 port
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
:
|
|
If you want to knwo more about GitHub SSH configuration, check out the documentation
5 How to check it?
In one Git repository, you can check this configuration typing the command:
Here an example for a GitHub repository
|
|
6 Conclusion
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!