这些天,当我在GitHub上的设置页面上创建一个新的存储库时,我得到:
git remote add origin https://github.com/nikhilbhardwaj/abc.git
git push -u origin master
每当我必须提交时,我需要输入我的GitHub用户名和密码。
我可以手动更改为
git@github.com:nikhilbhardwaj/abc.git
在.git/config。我发现这很烦人——有没有什么方法可以配置git默认使用SSH ?
你需要在ssh中克隆,而不是在https中。
$ ssh-keygen -t ed25519 -C "your_email@example.com"
添加~/.ssh/id_rsa的内容。在github.com上发布您的SSH密钥。
如果你需要为不同的主机设置不同的密钥,你可以使用这个脚本:
#!/usr/bin/env bash
if [ $# -lt 2 ]; then
echo "Provide email and hostname"
exit 1
fi
email="$1"
hostname="$2"
keypath="$HOME/.ssh/${hostname}_rsa"
ssh-keygen -t ed25519 -C $email -f $keypath
if [ ! $? -eq 0 ]; then
echo "Error when running ssh-keygen"
exit 1
fi
exit 0
cat >> $HOME/.ssh/config <<EOF
Host $hostname
Hostname $hostname *.$hostname
User git
IdentitiesOnly yes
IdentityFile $keypath
EOF
然后像这样运行
bash generate_ssh.sh your_email@example.com github.com
更改远程url
git remote set-url origin git@github.com:user/foo.git
(或者直接编辑。git/config)
将~/.ssh/github.com_rsa.pub的内容添加到github.com上的ssh密钥
检查连接
ssh -T git@github.com