git push 每次都要输入用户名和密码
原因是git clone URL使用的是HTTPS,需要更改为SSH,并更新本地的git地址。
Git push requires username and password
A common mistake is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking the ssh button left to the URL field and updating the URL of your origin remote like this:
git remote set-url origin [email protected]
完成上述两个步骤,再次push会提示一个错误:
Warning: Permanently added the RSA host key for IP address '' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
接下来需要按照官网的教程生成SSH key
这样就可以避免每次push需要输入用户名和密码。
整理于2015-03-21
多个SSH Key问题
但是一个SSH Key只能用于一个Github账号,如果试图把同一个key贴到另一个账号上,Github会提示错误。我们需要为不同的账号生成不同的key。
解决办法是建立配置文件~/.ssh/config
# GitHub Account 1
Host github-as-user1 // 这个名字在稍后用来测试连接:ssh -T [email protected]
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_1
# GitHub Account 2
Host github-as-user2
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_2
特别需要注意的是,此时~/.ssh/
路径下可能没有id_rsa_1
文件,所以在生成ssh key的命令中,需要加-f
选项指明文件,会新建不存在的文件,如果你尝试手动建立这个文件的话,会出现问题:
ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -C "[email protected]"
参考:
Multiple GitHub Accounts & SSH Config
git生成ssh key及本地解决多个ssh key的问题
多个key存在时,可能会push到错误的key账号
假如Github用户user1
把本地代码添加到一个远程仓库whatever.git
,并且命名为origin
,
git remote add origin [email protected]:user1/whatever.git
参考我们上面配置的~/.ssh/config
,可以把这行命令改写为:
git remote add gb-user1 [email protected]:user1/whatever.git
想要提交master
分支到user1/whatever
仓库时,就可以执行git push gh-user1 master
.
gh-user1
是远程仓库在本地的简称,可以改为你想要的名字,越简单明了越好,比如li2.me
,假如出现
fatal: remote gh-user1 already exists
可以git remote rm gh-user1
解决办法参考:
How to work with multiple ssh keys
整理于2015-03-28
全局性地更换电子邮件地址
$ git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
then
GIT_AUTHOR_NAME="new name";
GIT_AUTHOR_EMAIL="[email protected]";
git commit-tree "[email protected]";
else
git commit-tree "[email protected]";
fi' HEAD
git update-ref -d refs/original/refs/heads/master
git push --force --tags origin 'refs/heads/*'