Skip to content

Instantly share code, notes, and snippets.

@zevolution
Created January 26, 2025 12:12
Show Gist options
  • Save zevolution/411669468c501e84edb66c0ec91f575a to your computer and use it in GitHub Desktop.
Save zevolution/411669468c501e84edb66c0ec91f575a to your computer and use it in GitHub Desktop.
Multiple .gitconfig

Multiple .gitconfig

As a consultant, it's pretty common to juggle multiple clients throughout our workdayβ€”kinda like being a tech superhero, but without the cape (and the cool gadgets, sadly). With this tip, say goodbye to those endless local setups!

In the global .gitconfig do the following modifications:

[includeIf "gitdir:~/path/to/first-client/projects/"]
    path = ~/.gitconfig-first-client

[includeIf "gitdir:~/path/to/second-client/projects/"]
    path = ~/.gitconfig-second-client

Important

Pay attention slash(/) in the end of line, this assert that git recognize all sub-folders as mentioned in the documentation:

"If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively."

Now inside of the defined files ~/.gitconfig-first-client and ~/.gitconfig-second-client you can add different configs like:

# ~/.gitconfig-first-client
[user]
  name = JosΓ© Lucas
  email = [email protected]
# ~/.gitconfig-second-client
[user]
  name = Zevolution
  email = [email protected]
  signingkey = ssh-rsa AAAA...
[gpg]
  format = ssh
[commit]
  gpgsign = true

Now when you create a new repository, clone, commit or push a repo, inside the specified projects folder that we configure previously, Git will recognize the configurations by folder

This is very useful when we have something like the following structure:

πŸ“‚ /
┣ πŸ“‚ professional
┃ ┣ πŸ“‚ clients
┃ ┃ ┣ πŸ“‚ client-01
┃ ┃ ┃ ┣ πŸ“‚ projects
┃ ┃ ┃ ┃ ┣ πŸ“‚ project-01
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ controller
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ model
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ view
┃ ┃ ┃ ┃ ┃ β”— πŸ“œ README.md
┃ ┃ ┃ ┃ ┣ πŸ“‚ project-02
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ controller
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ model
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ view
┃ ┃ ┃ ┃ ┃ β”— πŸ“œ README.md
┃ ┃ ┣ πŸ“‚ client-02
┃ ┃ ┃ ┣ πŸ“‚ projects
┃ ┃ ┃ ┃ ┣ πŸ“‚ project-01
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ controller
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ model
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ view
┃ ┃ ┃ ┃ ┃ β”— πŸ“œ README.md
┃ ┃ ┃ ┃ ┣ πŸ“‚ project-01
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ controller
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ model
┃ ┃ ┃ ┃ ┃ ┣ πŸ“‚ view
┃ ┃ ┃ ┃ ┃ β”— πŸ“œ README.md 

By this way you don't care about configure scope local in all new projects!

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