Wednesday, January 11, 2023

How to Use .gitconfig's includeIf - Git 2.13 conditional config on windows - git set username and email for separate project - Is it possible to have different Git configuration for different projects?

.gitconfig is usually stored in the user.home directory.
I use a different identity to work on projects for Company A and something else for Company B (primarily the name / email). How can I have two different Git configurations so that my check-ins don't go with the name / email?
As of git version 2.13, git supports conditional configuration includes. In this example we clone Company A's repos in global directory anywhere in my computer, and Company B's repos in some other specific folder like D:/Mine/Projects. For company B, we will keep all our repo's inside D:/Mine/Projects
File Location For Windows : "C:\Users\${USER_NAME}\.gitconfig", follow below steps to make it work:
  • Open .gifconfig in a editor
  • Paste below contents as example:
    [include]
      path = .gituser-default
    [includeIf "gitdir:D:/Mine/Projects/"]
      path = .gituser-company-b	
    [credential]
    	helper = wincred
    
  • Now edit .gituser-default with below contents - this configuration will use for anywhere in your computer
    [user]
    	name = Pritom Kumar
    	email = pritomkucse@gmail.com
    
  • Now edit .gituser-company-b with below contents - this configuration will use for Company B
    [user]
    	name = Pritom Kumar
    	email = pritomku.test@gmail.com
    
  • Now clone an repo into the /D/Mine/Projects directory and navigate to the project directory
  • Now run command git config user.email to check the configuration, you will see as below:

No comments:

Post a Comment