RSS Amplifier

Random Thoughts · Dec 21, 2022

Use separate Git identities based on directories

0
Sign in to vote or save

Shirish Padalkar · Random Thoughts

This guide helps you set up different identities in Git while working on different projects based on the directory you are in.

Let’s assume your directory structure looks like this:

~
├── .gitconfig
└── Projects
    ├── company
    │   ├── company-project-1
    │   ├── compnay-project-2
    │   └── company-project-3
    ├── oss
    │   ├── oss-project-1
    │   └── oss-project-2
    ├── personal-project-1
    └── personal-project-2
  • You have global .gitconfig in the home directory. This contains your personal git configuration

  • You have a directory ~/Projects/company for all company repositories. You want to use your company identity for all these projects.

  • You have a directory ~/Projects/oss for all your open-source repositories. And you want to use your open-source identity for these projects.

  • You have cloned all other projects directly in the ~/Projects directory without any sub-directories. You want to use your personal identity for all these projects.

A typical ~/.gitconfig looks like this:

# ~/.gitconfig
[user]
    name = User Name
    email = personal-email@gmail.com
# some more common configuration

Let’s create 2 additional config files for different git identities.

# ~/.gitconfig-company
[user]
    name = User Name
    email = user-name@company.com
# ~/.gitconfig-oss
[user]
    name = User Name
    email = oss-email@oss-project.com

Update your ~/.gitconfig file to include these identity files based on the directory.

# ~/.gitconfig
[user]
    name = User Name
    email = personal-email@gmail.com
[includeIf "gitdir:~/Projects/company/"]
    path = .gitconfig-company
[includeIf "gitdir:~/Projects/oss/"]
    path = .gitconfig-oss
# some more common configuration

Please adjust the .gitconfig file names and directories in the above file based on your local setup

And that’s all. Now when you go into ~/Projects/company, Git will automatically load the configuration from the ~/.gitconfig-company file and append it to the existing configuration. And the same thing will happen for ~/Projects/oss projects.

No posts

Read the original on shirishpadalkar.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.