在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。
但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。
是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?
有点像Rob W的答案,但允许不同的ssh密钥,并适用于旧版本的git(没有例如核心)。sshCommand配置)。
我创建了~/bin/git_poweruser文件,具有可执行权限,并在PATH中:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
每当我想以“超级用户”的身份提交或推送一些东西时,我使用git_poweruser而不是git。它可以在任何目录下工作,并且不需要更改.gitconfig或.ssh/config,至少在我的目录中不需要更改。
Git别名(和Git配置中的部分)来拯救!
添加别名(从命令行):
git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
然后,以集合为例
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
在一个新的或克隆的repo中,你可以运行这个命令:
git identity github
这个解决方案不是自动的,而是在全局~/中重置用户和电子邮件。Gitconfig和设置用户。useConfigOnly设为true将迫使git提醒你在每次新的或克隆的repo中手动设置它们。
git config --global --unset user.name
git config --global --unset user.email
git config --global user.useConfigOnly true
也许这是一个简单的黑客,但它是有用的。只需生成如下所示的2个ssh密钥。
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
同样的方法为个人创造一个。因此,您有两个ssh密钥,work和company。复制工作。酒吧,工作,个人。Pub, personal to ~/。ssh /目录。
然后使用以下代码创建一个shell脚本,并将其命名为crev.sh (Company Reverse),并包含以下内容。
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
以同样的方式,用以下内容创建一个名为prew .sh(个人反向)的文件。
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
在~ /。Bashrc为这些脚本添加别名如下所示
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
无论何时你想使用company,只要使用crev,如果你想使用personal,请使用prev:-p。
将这些ssh密钥添加到您的GitHub帐户。请确保之前没有生成id_rsa,因为这些脚本将覆盖id_rsa。如果您已经生成了id_rsa,请将其用于其中一个帐户。将它们复制为个人密钥,跳过生成个人密钥。
有点像Rob W的答案,但允许不同的ssh密钥,并适用于旧版本的git(没有例如核心)。sshCommand配置)。
我创建了~/bin/git_poweruser文件,具有可执行权限,并在PATH中:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
每当我想以“超级用户”的身份提交或推送一些东西时,我使用git_poweruser而不是git。它可以在任何目录下工作,并且不需要更改.gitconfig或.ssh/config,至少在我的目录中不需要更改。