Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 6, 2020 07:24 pm GMT

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 wont 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 = [email protected]
Enter fullscreen mode Exit fullscreen mode
# ~/.gitconfig-work-b[user]    name = Scooby Doo, Food-Department    email = [email protected]
Enter fullscreen mode Exit fullscreen mode
# ~/.gitconfig-personal[user]    name = Scooby    email = [email protected]
Enter fullscreen mode Exit fullscreen mode

Thats 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, its 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!


Original Link: https://dev.to/flyingdot/configure-git-for-work-and-personal-stuff-434j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To