Using different Git config for personal and work projects

I use the same machines to work on both personal and work projects. I usually have to use a different Git identity for the work projects than for my personal projects.

Previously I had my personal Git identity set globally and then used local Git configs to override it in work projects. This worked just fine but it was too much work. There is a better solution.

Git config allows you to use, or better to say include, another Git config for a specific directory and all its subdirectories. I have all my projects stored in ~/Projects and subdirectories like ~/Projects/open-source and work projects in ~/Projects/CompanyName.

I created a ~/.companyName.gitconfig that overrides just the name, email and GPG signing key to match the work identity

[user]
    name = Igor Kulman
    email = igor@company.name
    signingkey = ABC

I then included this config in my main ~/.gitconfig just for the ~/Projects/CompanyName directory

[user]
	name = Igor Kulman
	email = igor@kulman.sk
    signingkey = DEF
...
[includeIf "gitdir:~/Projects/CompanyName/"]
    path = ~/.companyName.gitconfig

to achieve exactly what I needed.

To verify and quickly check which Git identity is being used in a specific Git repository you can use this simple Git alias

[alias]    
    whoami = "! git var -l | grep '^GIT_.*_IDENT'"
macOS  Xcode 

Using Visual Studio Code as Git merge tool

Visual Studio Code is a neat editor with many good functionalities but I especially like the way it shows Git merge conflicts. Instead of a 2-way or a 3-way split it just shows one window with both changes, nicely highlighted with colors and actions.

I like this feature so much I decided to set Visual Studio Code as my Git merge tool for both the command line and Git Tower.

Command line

Using Visual Studio Code as a merge tool for Git when using command line means editing your .gitconfig. You just need to define a new tool called code and set it as the default merge tool.

[merge]
        tool = vscode
[mergetool "vscode"]
        cmd = code --wait $MERGED
[Read More]
Git  macOS  VSCode