Skip to content

Instantly share code, notes, and snippets.

@Icaruk
Last active May 9, 2024 19:45
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@atwork.com

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 = myemail@hotmail.com

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 "myemail@hotmail.com"

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 = myemail@hotmail.com

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

MacOS and Linux:

[user]
	name = Icaruk
	email = myemail@hotmail.com

[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.

@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