解決啟用 GitHub 隱私郵箱后推送錯(cuò)誤的問題
如果使用GitHub Desktop軟件提交可能會(huì)報(bào)以下錯(cuò)誤
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,可能會(huì)出現(xiàn)以下錯(cuò)誤
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.
如果你已經(jīng)設(shè)置了隱私郵箱,并且已經(jīng)使用了,但是還是無法提交
取消勾選
Block command line pushes that expose my email選項(xiàng),允許你的私人郵箱也可以推送,但是可能還是會(huì)暴露你的郵箱。如果想繼續(xù)使用隱私郵箱,在 Settings → Emails中,保持勾選
Keep my email addresses private和Block command line pushes that expose my email選項(xiàng),
找到你的 GitHub 隱私郵箱,它在Keep my email addresses private的描述中。

- 打開終端,在 Git 中設(shè)置全局郵箱。使用 GitHub 提供的隱私郵箱。
git config --global user.email "{ID}+{username}@users.noreply.github.com"
- 在單個(gè)倉(cāng)庫中設(shè)置隱私郵箱。
git config user.email "{ID}+{username}@users.noreply.github.com"
- 重置上次提交的作者信息。
git commit --amend --reset-author --no-edit
- 當(dāng)有多個(gè) Git 賬號(hào)時(shí),并設(shè)置了多個(gè)
SSH-Key,在~/.ssh目錄下找到config文件,并在每個(gè) Host 下,添加User {ID}+{username}@users.noreply.github.com。
如果還不會(huì)給 Git 設(shè)置多個(gè)SSH-Key,請(qǐng)參考Git 配置多個(gè) 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
目前還是比較推薦第三種方法的,對(duì)于多 Git 賬號(hào)的人,可以只在~/.ssh/config添加User user即可推送。親測(cè)可用。
參考資料
Error "Your push would publish a private email address"
How to amend several commits in Git to change author
Git 配置多個(gè) SSH-Key