解决启用GitHub隐私邮箱后推送错误的问题
解决启用 GitHub 隐私邮箱后推送错误的问题
如果使用GitHub Desktop
软件提交可能会报以下错误
fatal: unable to access 'https://github.com/username/project.git/': Failed to connect to github.com port 443 after 75003 ms: Couldn't connect to server
如果使用终端
或iTerm2
,可能会出现以下错误
Cannot push these commits as they contain an email address marked as private on GitHub. To push anyway, visit https://github.com/settings/emails, uncheck "Keep my email address private", then switch back to GitHub Desktop to push your commits. You can then enable the setting again.
如果你已经设置了隐私邮箱,并且已经使用了,但是还是无法提交
-
取消勾选
Block command line pushes that expose my email
选项,允许你的私人邮箱也可以推送,但是可能还是会暴露你的邮箱。 -
如果想继续使用隐私邮箱,在 Settings → Emails中,保持勾选
Keep my email addresses private
和Block command line pushes that expose my email
选项,
找到你的 GitHub 隐私邮箱,它在Keep my email addresses private
的描述中。
- 打开终端,在 Git 中设置全局邮箱。使用 GitHub 提供的隐私邮箱。
git config --global user.email "{ID}+{username}@users.noreply.github.com"
- 在单个仓库中设置隐私邮箱。
git config user.email "{ID}+{username}@users.noreply.github.com"
- 重置上次提交的作者信息。
git commit --amend --reset-author --no-edit
- 当有多个 Git 账号时,并设置了多个
SSH-Key
,在~/.ssh
目录下找到config
文件,并在每个 Host 下,添加User {ID}+{username}@users.noreply.github.com
。
如果还不会给 Git 设置多个SSH-Key
,请参考Git 配置多个 SSH-Key。
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
User {ID}+{username}@users.noreply.gitee.com
Host github.com
Hostname ssh.github.com
Port 443
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
User {ID}+{username}@users.noreply.github.com
目前还是比较推荐第三种方法的,对于多 Git 账号的人,可以只在~/.ssh/config
添加User user
即可推送。亲测可用。
参考资料
Error "Your push would publish a private email address"
How to amend several commits in Git to change author
Git 配置多个 SSH-Key