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.

@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