Skip to content

Instantly share code, notes, and snippets.

@Icaruk
Last active September 16, 2024 20:55
Show Gist options
  • Save Icaruk/f024a18093dc28ec1588cfb90efc32f7 to your computer and use it in GitHub Desktop.
Save Icaruk/f024a18093dc28ec1588cfb90efc32f7 to your computer and use it in GitHub Desktop.
How to have multiple profiles on git

Last update: 30-01-2024
Last view: 30-01-2024

Step 1

Go to your work folder, mine is located at: F:/Work/EnterpriseName/

And then create a .gitconfig-work with the following data:

[user]
	name = Name at work
	email = [email protected]

The folder should be like this:

📂 EnterpriseName
├─ 📂 project01
├─ 📂 project02
├─ 📂 project03
└─ 📄 .gitconfig-work

Step 2

Navigate to your current .gitconfig and open it. Usually it's at C:\Users\your-user

It should have something like:

[user]
	name = Icaruk
	email = [email protected]

That's the global config, that will be used as the default one. If it doesn't exist, create it with these commands:

> git config --global user.name "Icaruk"
> git config --global user.email "[email protected]"

Step 3

Add the following lines below:

[includeIf "gitdir:F:/Work/EnterpriseName/"]
	path = F:/Work/EnterpriseName/.gitconfig-work
  • The first path is your work path, it must end with "/".
  • The second path is your .gitconfig-work that you created on your work folder. But it can be anywhere, just point at it.

⚠️ The paths are case sensitive, double check if it doesn't work.
⚠️ The includeIf overrides the configuration above it, so It must be placed below the [user] data block. More info at mi6th comment.

The resulting file should be like this:

Windows:

[user]
	name = Icaruk
	email = [email protected]

[includeIf "gitdir:F:/Work/EnterpriseName/"]
	path = F:/Work/EnterpriseName/.gitconfig-work

MacOS and Linux:

[user]
	name = Icaruk
	email = [email protected]

[includeIf "gitdir:~/Work/EnterpriseName/"]
	path = ~/Work/EnterpriseName/.gitconfig-work

If it doesn't work, try removing the ~

Step 4

Go to your work folder and open any project, then run:

> git config user.email

It should display your work email.

Now go to any project that isn't located inside your work folder and run the same command. It should display your default email.

⚠️ You must be in a folder with git initialized in order to see the overriden config, otherwise the global config will be shown.

@aperedag
Copy link

In the step 2, email of .gitconfig no is the same as of command's value (git config --global user.email). Missing 'my'

@Icaruk
Copy link
Author

Icaruk commented Dec 23, 2021

@aperedag fixed, thanks 🙂

@giannifontanot
Copy link

giannifontanot commented Jan 6, 2022

Thank you a lot for creating this Gist. This issue was driving me crazy...

@Icaruk
Copy link
Author

Icaruk commented Jan 6, 2022

@giannifontanot you're welcome :)

@lb12
Copy link

lb12 commented Apr 12, 2022

Awesome! Thank you! ✌🏼

@anevolbap
Copy link

like a charm! thank you!

@0x6d6c
Copy link

0x6d6c commented Jul 14, 2022

Note that the includeIf directive(s) MUST FOLLOW configuration values that should be overridden by the included file, e.g. this won't work:

# ~/.gitconfig file
# Let's say we keep configuration sections in alphabetical order: [includeIf] precedes [user]

[includeIf "gitdir:F:/Work/EnterpriseName/"]
	path = F:/Work/EnterpriseName/.gitconfig-work
[init]
    defaultBranch = main

# Let's assume this is our default user for all repositories
[user]
	name = defaultUser
	email = [email protected]

Now, if we run the command git -C F:/Work/EnterpriseName/MySomeCoolRepo config --get user.name the result is defaultUser and not Icaruk because the value defaultUser for user.name hasn't been overridden by the included file as desired.

To fix this we need to reorder declarations in the ~/.gitconfig file like this:

# Fixed ~/.gitconfig file
[init]
    defaultBranch = main

# Let's assume this is our default user for all repositories
[user]
	name = defaultUser
	email = [email protected]

# Let's assume this file contains the '[user]' as in this gist
# and we want to use it in any repository under F:/Work/EnterpriseName/
[includeIf "gitdir:F:/Work/EnterpriseName/"]
	path = F:/Work/EnterpriseName/.gitconfig-work

Now, if we run the command git -C F:/Work/EnterpriseName/MySomeCoolRepo config --get user.name the result is Icaruk and no more defaultUser because the value of the user.name directive has been overridden by the included file as desired.

@Icaruk
Copy link
Author

Icaruk commented Jul 14, 2022

Note that the includeIf directive(s) MUST FOLLOW configuration values that should be overridden by the included file, e.g. this won't work:

Thanks for your contribution, I'm adding your comment to the gist :)

@rsimonton
Copy link

The includeIf overrides the configuration above it, so It must be placed above the [user] data block.

I think you meant below the user data block.

@Icaruk
Copy link
Author

Icaruk commented Aug 6, 2022

I think you meant below the user data block.

@rsimonton Nice catch, thanks!

@nour-s
Copy link

nour-s commented Aug 13, 2022

This is awesome.
Please mention that the drive letter is case-sensitive :(, it took me hours to figure it out.

@Icaruk
Copy link
Author

Icaruk commented Aug 14, 2022

This is awesome. Please mention that the drive letter is case-sensitive :(, it took me hours to figure it out.

I'll add a warning, thanks!

@huynle
Copy link

huynle commented Nov 2, 2022

and symlinked folder wont work! i just verified it. thanks for the Gist!

@putnikproj
Copy link

Thank you so much! I am really grateful to you. Nothing worked for me except your guide

@Icaruk
Copy link
Author

Icaruk commented Jan 15, 2023

and symlinked folder wont work! i just verified it. thanks for the Gist!

Thanks!

Thank you so much! I am really grateful to you. Nothing worked for me except your guide

You are welcome!

Copy link

ghost commented Feb 24, 2023

Took me a few hours to figure out why it doesn't work. The overridden config will only be visible when you are in a git directory, otherwise it always shows the global values.

@Icaruk
Copy link
Author

Icaruk commented Feb 24, 2023

Took me a few hours to figure out why it doesn't work. The overridden config will only be visible when you are in a git directory, otherwise it always shows the global values.

I'll add a warning on that, thanks!

@sastorsl
Copy link

Thanks for this gist.

For Mac and Linux users - running from a shell - there are a couple of caveats:

  1. The home directory must be written as ~ (not $HOME), both in the includeIf section and in the path value.
  2. For sub directories to work you must have a trailing slash
# ~/.gitconfig
[user]
  name = User Name
  email = [email protected]

[includeIf "gitdir:~/git/github.com/"]
  path = ~/.gitconfig.github

git config gitdir

@Icaruk
Copy link
Author

Icaruk commented Nov 25, 2023

Thanks for this gist.

For Mac and Linux users - running from a shell - there are a couple of caveats:

  1. The home directory must be written as ~ (not $HOME), both in the includeIf section and in the path value.
  2. For sub directories to work you must have a trailing slash
# ~/.gitconfig
[user]
  name = User Name
  email = [email protected]

[includeIf "gitdir:~/git/github.com/"]
  path = ~/.gitconfig.github

git config gitdir

I included that info, thanks!

@vegerot
Copy link

vegerot commented Apr 5, 2024

Question: is it possible to use a [includeIf] section with any other config than path? For example, I want to do

[includeIf "gitdir:~/work-stuff/"]
  [user]
    email = [email protected]
  [commit]
    gpgSign

However, this doesn't seem to be working. Is what I want to do impossible?

@0x6d6c
Copy link

0x6d6c commented Apr 5, 2024

@eshimischi
Copy link

eshimischi commented Apr 9, 2024

Here is how to use it with remote repo services like gitlab.com, for instance https://docs.gitlab.com/ee/user/project/repository/signed_commits/gpg.html#set-signing-key-conditionally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment