Skip to content

Instantly share code, notes, and snippets.

@philippschmalen
Created November 20, 2021 09:05
Show Gist options
  • Save philippschmalen/a631e6b9d03494a23d964515df9d15be to your computer and use it in GitHub Desktop.
Save philippschmalen/a631e6b9d03494a23d964515df9d15be to your computer and use it in GitHub Desktop.

Git config for folders and multiple accounts

You have several github profiles for different uses. For example, one account used privately and another account used professionally. Instead of switching profiles all the time which might be error prone, we can setup different git profiles for each folder.

TL;DR

Go to your user home folder. Open .gitconfig, edit and add includeIf which points to private.gitconfig.

Create private.gitconfig and add your github name and email as below. These settings point towards any directory within */projects/private

# .gitconfig
[user]
	name = philippbusiness
	email = [email protected]
[includeIf "gitdir/i:projects/private/"]
    path = private.gitconfig

# private.gitconfig
[user]
	name = philippprivate
	email = [email protected]

Create private.gitconfig in the same folder where .gitignore is. Run these checks to verify the setup:

# navigate to a git repo outside of */projects/private
git config user.name
> philippbusiness

# navigate to a git repo within */projects/private
git config user.name
> philippprivate

Further notes:

  • adding /i makes the path case-insensititve
  • having / at the end of "gitdir/i:projects/private/" includes all files and subdirectories recursively
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment