Is it possible to have different Git configuration for different projects?

前端 未结 12 734
萌比男神i
萌比男神i 2020-12-02 03:44

.gitconfig is usually stored in the user.home directory.

I use a different identity to work on projects for Company A and something else fo

相关标签:
12条回答
  • 2020-12-02 03:53

    To be explicit, you can also use --local to use current repository config file:

    git config --local user.name "John Doe" 
    
    0 讨论(0)
  • 2020-12-02 03:54

    As of git version 2.13, git supports conditional configuration includes. In this example we clone Company A's repos in ~/company_a directory, and Company B's repos in ~/company_b.

    In your .gitconfig you can put something like this.

    [includeIf "gitdir:~/company_a/"]
      path = .gitconfig-company_a
    [includeIf "gitdir:~/company_b/"]
      path = .gitconfig-company_b
    

    Example contents of .gitconfig-company_a

    [user]
    name = John Smith
    email = john.smith@companya.net
    

    Example contents of .gitconfig-company_b

    [user]
    name = John Smith
    email = js@companyb.com
    
    0 讨论(0)
  • 2020-12-02 04:04

    You can also point the environment variable GIT_CONFIG to a file that git config should use. With GIT_CONFIG=~/.gitconfig-A git config key value the specified file gets manipulated.

    0 讨论(0)
  • 2020-12-02 04:04

    Another way is to use direnv and to separate config files per directory. For example:

    .
    ├── companyA
    │  ├── .envrc
    │  └── .gitconfig
    ├── companyB
    │  ├── .envrc
    │  └── .gitconfig
    └── personal
       ├── .envrc
       └── .gitconfig
    

    Each .envrc should contain something like this:

    export GIT_CONFIG=$(pwd)/.gitconfig
    

    And .gitconfig is usual gitconfig with desired values.

    0 讨论(0)
  • 2020-12-02 04:11

    Thanks @crea1

    A small variant:

    As it is written on https://git-scm.com/docs/git-config#_includes:

    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.

    So I use in my case,
    ~/.gitconfig :

    [user] # as default, personal needs
        email = myalias@personal-domain.fr
        name = bcag2
    [includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
        path = .gitconfig-job
    
    # all others section: core, alias, log…
    

    So If the project directory is in my ~/wokspace/, default user settings is replace with
    ~/.gitconfig-job :

    [user]
    name = John Smith
    email = js@company.com
    
    0 讨论(0)
  • 2020-12-02 04:11

    Follow the Steps:

    1. Find .gitconfig from the system

      File Location For Windows : "C:\Users${USER_NAME}.gitconfig"

      File Location For Linux : "/usr/local/git/etc/gitconfig"

    2. Open .gitconfig file and add below lines as per your condition

       [includeIf "gitdir:D:\ORG-A-PROJECTS\"]
      
       [user]
      
           name = John Smith
      
           email = js@organizationx.com [includeIf "gitdir:~/organization_b/"]
      
       [user]
      
           name = John Doe
      
           email = jd@organizationy.com
      
    0 讨论(0)
提交回复
热议问题