不同项目可以有不同的Git配置吗?

650

.gitconfig 通常存储在 user.home 目录中。

我在为公司A和公司B的项目工作时使用不同的身份(主要是姓名/电子邮件)。如何使用两个不同的Git配置,以便我的提交不会使用我的姓名/电子邮件?

15个回答

2

按照以下步骤操作:

  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
    

Linux中的文件位置错误,特别是对于Ubuntu 18或20而言,在/usr/local/中没有git文件夹! - bcag2

1

0

在尝试git stash本地更改时,我遇到了一个错误。Git的错误信息是“请告诉我你是谁”,然后告诉我运行git config --global user.email "you@example.com"git config --global user.name "Your name"来设置您帐户的默认身份。但是,您必须省略--global仅在当前存储库中设置身份。


0
我和你一样。我写了一个小的bash脚本来管理它们。 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

你的脚本是 Bash 版本,在 Github 上有 Python 版本。此外,-s 在你的 Bash 脚本中没有被处理。 - Vadim Kotov

0
如果有人在Windows上遇到问题,请按照以下步骤操作:
编辑文件%USERPROFILE%\.gitconfig,并为每个您想要设置特定配置的公司添加以下行。
[includeIf "gitdir:C:\\!work\\company_X\\"]

[user]    
     name = Some Guy    
     email = some.guy@X.com 

[includeIf "gitdir:C:\\!work\\company_Y\\"]

[user]    
     name = Some Other Guy    
     email = some.other.guy@Y.com 

那对我来说有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接