DEV Community

Cover image for Configure Git for Work and Personal stuff
Dani Meier
Dani Meier

Posted on

Configure Git for Work and Personal stuff

Photo by Maddy Baker on Unsplash

If you are like me and work for different companies, have several side hustles and fun projects, you will probably have the same problem as I have:
You want to use different configurations, like E-Mail Addresses, for those repositories.
Of course, you can always apply a local configuration to each repository or even use some scripts or aliases to help you out. But again, if you are like me, you won’t be happy with it, because you will forget to apply it over and over again.

Conditional Configuration

Git allows you to include external configuration-files based on conditions, like the current directory. With this in mind, the problem above is actually easily solvable.

First use different base-directories for each “area”, like your different work-places and personal stuff:

/home/me/
    dev-work-a/
    dev-work-b/
    dev-personal/
Enter fullscreen mode Exit fullscreen mode

Then create a .gitconfig-file for each of those areas:

/home/me/
    .gitconfig (your main configuration)
    .gitconfig-work-a (specific configuration for work a)
    .gitconfig-work-b (specific configuration for work b)
    .gitconfig-personal (specific configuration for personal projects)
Enter fullscreen mode Exit fullscreen mode

In your main .gitconfig-File, instead of the actual values, you conditionally include the other files:

# ~/.gitconfig
[includeIf "gitdir:~/dev-work-a/"]
    path = .gitconfig-work-a
[includeIf "gitdir:~/dev-work-b/"]
    path = .gitconfig-work-b
[includeIf "gitdir:~/dev-personal/"]
    path = .gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

and in the specific files, you just define the specific configuration values:

# ~/.gitconfig-work-a
[user]
    name = Scooby Doo
    email = scooby@worka.com
Enter fullscreen mode Exit fullscreen mode
# ~/.gitconfig-work-b
[user]
    name = Scooby Doo, Food-Department
    email = scooby@workb.com
Enter fullscreen mode Exit fullscreen mode
# ~/.gitconfig-personal
[user]
    name = Scooby
    email = me@scoobydoo.com
Enter fullscreen mode Exit fullscreen mode

That’s basically it. So now, when you are for example in ~/dev-personal/bigbang-side-hustle Git will get your user-configuration from .gitconfig-personal.

One more thing

Well, of course, you cannot use conditional configuration for user-configurations like name and e-mail only, it’s just a very common use case. But in fact, you can use it for whatever configuration you like.

There are also two more conditions like gitdir:

  • onbranch lets you apply configurations based on the branch you are on
  • gitdir/i like gitdir, but case-insensitive

It's worth mentioning that this works on all platforms and is very Dotfiles-friendly.
Also see documentation for more information.

Have fun!

Top comments (9)

Collapse
 
vlasales profile image
Vlastimil Pospichal • Edited

I use instead

dev-work-a/.git/config
dev-work-b/.git/config
dev-personal/.git/config
Enter fullscreen mode Exit fullscreen mode
Collapse
 
flyingdot profile image
Dani Meier

Can you explain little more about your solution? I don't think that this is working just by "injecting" additional partial .gitconfig-Files into the file-tree.

Collapse
 
vlasales profile image
Vlastimil Pospichal

Uhh, sorry. The path is .git/config.

Each project can have its own .git/config. This is described in the manual as a local configuration that overrides the same keys in the global config.

Collapse
 
ravishankar140 profile image
ravi shankar

Instead use --local flag, This will override the global config.

cd dev-work-a
git config --local user.name "xyz"
git config --local user.email xyz@abc.com
Enter fullscreen mode Exit fullscreen mode

git config --global writes the config to ~/.gitconfig and git config --local writes to .git/config

Collapse
 
flyingdot profile image
Dani Meier • Edited

Well, this is what I defined in the article with "you can always apply a local configuration to each repository". That's ok if you have one or two repositories to apply this configuration to, but if you have dozens or more then this is just not feasible anymore - at least for me.

With the solution in the article, one can apply those configurations as defaults for all of your repositories in the corresponding base-folder. So both ways are totally fine, it just depends on the situation, which one suits better.

Collapse
 
adityamangal1 profile image
Aditya Mangal

Much helpful!

Collapse
 
ucavalcante profile image
Ulisses Cavalcante

Very useful tip, tks man.

Collapse
 
levirs565 profile image
Levi Rizki Saputra

Great post

Collapse
 
mark_codes_the_web profile image
Mark Codes the Web

I love this, just starting to use git in personal projects and this is really helpful.