Git Pull via HTTPS, Git Push over SSH

This article assumes you have already setup SSH for GitHub or other Git site.

For many public Git repos, using HTTPS for git fetch, git pull and the like is adequate. These operations occur over HTTPS perhaps twice as fast as over SSH and with lower CPU utilization. However, SSH can provide enhanced security for git push operations. Git has intrinsic functionality for this setup in a global configuration.

One-time setup: SSH git push

  • when cloning a public repo, use https:// for the remote repo URL instead of ssh://.
  • to fix existing local public repos, edit individual repo .git/config accordingly

one-time:

git config --global url."ssh://github.com/".pushInsteadOf https://github.com/

This makes all GitHub (or GitLab, etc.) public repos push over SSH, unless overridden in a particular repo’s push configuration. This is confirmed by git remote -v in a repo, it will show the effect of the global ~/.gitconfig.

Troubleshooting

If you get error on git push like:

ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

The cause is probably in the individual repo .git/config, be sure the url is like:

[remote "origin"]
	url = https://github.com/username/repo.git

In particular, the ending of the url line must NOT have a trailing slash like repo/