在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。

但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。

是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?


当前回答

在阅读了许多答案后,下面是完整的步骤

如何为不同的github帐户设置多个SSH密钥

您可能需要开始检查当前保存的密钥

$ ssh-add -l

如果您决定删除之前的所有缓存键(可选,请注意这一点)

$ ssh-add -D

然后,您可以创建一个ssh pub/priv密钥链接到您希望/需要使用的每个电子邮件/帐户

$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"

执行这些命令后,您将创建以下文件

~/.ssh/id_rsa_work      
~/.ssh/id_rsa_work.pub

~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub 

确保身份验证代理程序正在运行

$ eval `ssh-agent -s`

添加生成的键,如下所示(从~/。ssh文件夹)

$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers

现在您可以再次检查保存的密钥

$ ssh-add -l

现在您需要将生成的公钥添加到您的github/ bickbucket服务器访问密钥

克隆每个回购到不同的文件夹

转到用户work将要工作的文件夹并执行此操作

$ git config user.name "Working Hard"
$ git config user.email "work@company.com" 

看看这是怎么做的检查"。git/config"的内容

转到用户pers将工作的文件夹并执行此操作

$ git config user.name "Personal Account"
$ git config user.email "pers@email.com" 

看看这是怎么做的检查"。git/config"的内容

完成这些之后,你就可以通过在这两个文件夹之间切换来提交你的个人代码和工作代码了

如果你正在使用Git Bash并且需要在Windows下生成ssh密钥,请遵循以下步骤:

https://support.automaticsync.com/hc/en-us/articles/202357115-Generating-an-SSH-Key-on-Windows

其他回答

本地邮箱/邮箱/ bashrc

.bashrc_local:不要跟踪这个文件,只把它放在你的工作电脑上:

export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"

.bashrc:跟踪这个文件,让它在工作电脑和家用电脑上都一样:

F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
    . "$F"
fi

我正在使用https://github.com/technicalpickles/homesick来同步我的dotfiles。

如果只有git配置可以接受环境变量:git配置中的Shell变量扩展

可以有多种方法来做到这一点,但我通过下面的shell函数来实现这一点。

function gitprofile() { if [[ $1 == "private" ]]; then if grep -q "tom@workmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi elif [[ $1 == "public" ]]; then if grep -q "tom@gmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi }

比如,

gitprofile "public" ->这将切换到配置tom@gmail.com和

gitprofile "private" ->这会切换到tom@workmail.com。

将此函数添加到~/。Bash_profile或~/。ZSHRC基于您当前的终端偏好。重新启动终端或像这样编译脚本

. ~ / . bash_profile

使函数有效。希望能有所帮助!

Windows环境

另外,如果您在系统中安装了Git Extensions -> Settings -> Global Settings,则可以修改此选项。

gitextensions-latest-release

右键单击Windows环境中的文件夹/目录以访问这些设置。

更新:如何在2.49版本中切换/维护多个设置

只需将此添加到~/。Bash_profile在github.com的默认键之间切换

# Git SSH keys swap
alias work_git="ssh-add -D  && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"

虽然大多数问题在某种程度上回答了OP,但我必须自己完成这个过程,甚至不用谷歌,我就能找到最快最简单的解决方案。以下是简单的步骤:

从其他回购中复制现有的.gitconf 粘贴到您新添加的回购 修改.gitconfig文件中的值,如名称、电子邮件和用户名 (用户) 名字=约翰 邮箱= john@email.net 用户名= john133 将filename添加到.gitignore列表中,以确保不会将.gitconfig文件提交到工作回购中