Skip to content

Instantly share code, notes, and snippets.

@PickleBoxer
Forked from Icaruk/multipleGitProfiles.md
Created May 7, 2024 10:31
Show Gist options
  • Save PickleBoxer/8c382becdd51cb60f8f4150bf2d2327e to your computer and use it in GitHub Desktop.
Save PickleBoxer/8c382becdd51cb60f8f4150bf2d2327e 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.

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