.gitconfig通常存储在用户中。主目录。

我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?


当前回答

我也有同感。我写了一个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

其他回答

找到了一个非常有用的shell脚本,覆盖了ssh密钥生成的所有过程,并一次又一次地输入长命令

注意:这仅适用于ZSh用户

https://github.com/tw-yshuang/Git_SSH-Account_Switch

由于@crea1

一个小变种:

这篇文章写在https://git-scm.com/docs/git-config#_includes:上

如果模式以/结尾,则会自动添加**。例如,模式foo/变成了foo/**。换句话说,它递归地匹配foo和里面的所有东西。

在我的例子中, ~ /。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…

因此,如果项目目录在我的~/wokspace/,默认用户设置将替换为 ~ /。gitconfig-job:

[user]
name = John Smith
email = js@company.com

git配置有3个级别;项目、全局、系统。

项目:项目配置仅对当前项目可用,并存储在项目目录下的.git/config中。 global:全局配置可用于当前用户的所有项目,并存储在~/.gitconfig中。 system:所有用户/项目的系统配置都可用,存储在/etc/gitconfig中。

创建一个项目特定的配置,你必须在项目的目录下执行:

$ git config user.name "John Doe" 

创建全局配置:

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

创建一个系统配置:

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

正如你可能猜到的,项目覆盖全局和全局覆盖系统。

注意:项目配置是本地的,只针对这个特定的repo的一个特定副本/克隆,如果从远程重新克隆了这个repo,则需要重新应用。它修改没有通过提交/推送发送到远程的本地文件。

使用Github配置添加多个SSH密钥

2021年8月13日之后,git不支持HTTPs认证方法,所以我认为这个答案需要更新。

遵循以下步骤:

删除保存在~/. SSH /目录下的所有SSH密钥(公共和私有)。 创建新的SSH密钥:

ssh-keygen -t rsa -b 4096 -C "account1@gmail.com"
ssh-keygen -t rsa -b 4096 -C "account2@gmail.com"

当询问文件名时,给出默认的~/。为account1和~/配置Ssh /id_rsa。Ssh /id_rsa_acc2对应account2。

启动ssh-agent,为account1和account2添加私钥:

eval `ssh-agent`
ssh-add -k ~/.ssh/id_rsa
ssh-add -k ~/.ssh/id_rsa_acc2

使用ssh-add -l命令确认添加密钥

复制account1和account2的公钥,并将其添加到您的github帐户。命令:cat ~/.ssh/id_rsa。Pub | pbcopy(检查是否复制:pbpaste 将account1的用户名和用户邮箱设置为GitHub全局配置:

git config --global user.name "acc1_username"
git config --global user.email "account1@gmail.com"

创建~ /。Ssh /config文件,配置如下:

# account1 github
Host github.com
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa

# account2 github
Host github.com-acc2
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa_acc2

要为一个项目设置GitHub account2,在项目根目录中设置项目级别的用户名和电子邮件:

git config user.name "acc2_username"
git config user.email "account2@gmail.com"

现在通过使用GitHub repo的SSH链接克隆或添加起源:

# for account1 repo
git remote set-url origin git@github.com:acc1_username/reponame.git

# for account2 repo
git clone git@github.com-acc2:acc2_username/reponame.git

如果有任何疑问,请随意添加评论。

我也有同感。我写了一个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