解決啟用 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,可能會出現(xiàn)以下錯誤
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選項,允許你的私人郵箱也可以推送,但是可能還是會暴露你的郵箱。如果想繼續(xù)使用隱私郵箱,在 Settings → Emails中,保持勾選
Keep my email addresses private和Block command line pushes that expose my email選項,
找到你的 GitHub 隱私郵箱,它在Keep my email addresses private的描述中。

- 打開終端,在 Git 中設(shè)置全局郵箱。使用 GitHub 提供的隱私郵箱。
git config --global user.email "{ID}+{username}@users.noreply.github.com"
- 在單個倉庫中設(shè)置隱私郵箱。
git config user.email "{ID}+{username}@users.noreply.github.com"
- 重置上次提交的作者信息。
git commit --amend --reset-author --no-edit
- 當有多個 Git 賬號時,并設(shè)置了多個
SSH-Key,在~/.ssh目錄下找到config文件,并在每個 Host 下,添加User {ID}+{username}@users.noreply.github.com。
如果還不會給 Git 設(shè)置多個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