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

前端 未结 12 736
萌比男神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 04:12

    I had an error when trying to git stash my local changes. The error from git said "Please tell me who you are" and then told me to "Run git config --global user.email "you@example.com and git config --global user.name "Your name" to set your account's default identity." However, you must Omit --global to set the identity only in your current repository.

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

    I'm in the same boat. I wrote a little bash script to manage them. https://github.com/thejeffreystone/setgit

    #!/bin/bash
    
    # setgit
    #
    # Script to manage multiple global gitconfigs
    # 
    # To save your current .gitconfig to .gitconfig-this just run:
    # setgit -s this
    #
    # To load .gitconfig-this to .gitconfig it run:
    # setgit -f this
    # 
    # 
    # 
    # Author: Jeffrey Stone <thejeffreystone@gmail.com>
    
    usage(){
      echo "$(basename $0) [-h] [-f name]" 
      echo ""
      echo "where:"
      echo " -h  Show Help Text"
      echo " -f  Load the .gitconfig file based on option passed"
      echo ""
      exit 1  
    }
    
    if [ $# -lt 1 ]
    then
      usage
      exit
    fi
    
    while getopts ':hf:' option; do
      case "$option" in
          h) usage
             exit
             ;;
          f) echo "Loading .gitconfig from .gitconfig-$OPTARG"
             cat ~/.gitconfig-$OPTARG > ~/.gitconfig
             ;;
          *) printf "illegal option: '%s'\n" "$OPTARG" >&2
             echo "$usage" >&2
             exit 1
             ;;
        esac
    done
    
    0 讨论(0)
  • 2020-12-02 04:15

    The .git/config file in a particular clone of a repository is local to that clone. Any settings placed there will only affect actions for that particular project.

    (By default, git config modifies .git/config, not ~/.gitconfig - only with --global does it modify the latter.)

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

    I am doing this for my email in the following way:

    git config --global alias.hobbyprofile 'config user.email "me@example.com"'
    

    Then when I clone a new work project, I have only to run git hobbyprofile and it will be configured to use that email.

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

    You can customize a project's Git config by changing the repository specific configuration file (i.e. /path/to/repo/.git/config). BTW, git config writes to this file by default:

    cd /path/to/repo
    git config user.name 'John Doe'  # sets user.name locally for the repo
    

    I prefer to create separate profiles for different projects (e.g. in ~/.gitconfig.d/) and then include them in the repository's config file:

    cd /path/to/repo
    git config include.path '~/.gitconfig.d/myproject.conf'
    

    This works well if you need to use the same set of options in multiple repos that belong to a single project. You can also set up shell aliases or a custom Git command to manipulate the profiles.

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

    There are 3 levels of git config; project, global and system.

    • project: Project configs are only available for the current project and stored in .git/config in the project's directory.
    • global: Global configs are available for all projects for the current user and stored in ~/.gitconfig.
    • system: System configs are available for all the users/projects and stored in /etc/gitconfig.

    Create a project specific config, you have to execute this under the project's directory:

    $ git config user.name "John Doe" 
    

    Create a global config:

    $ git config --global user.name "John Doe"
    

    Create a system config:

    $ git config --system user.name "John Doe" 
    

    And as you may guess, project overrides global and global overrides system.

    Note: Project configs are local to just one particular copy/clone of this particular repo, and need to be reapplied if the repo is recloned clean from the remote. It changes a local file that is not sent to the remote with a commit/push.

    0 讨论(0)
提交回复
热议问题