Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSH option in gitconfig #397

Open
cgimenes opened this issue Aug 28, 2024 · 3 comments
Open

SSH option in gitconfig #397

cgimenes opened this issue Aug 28, 2024 · 3 comments

Comments

@cgimenes
Copy link

Github recommends to use SSH over HTTPS, and some people like me uses only SSH to clone repos.
So, it would be good to have a setting in the .gitconfig to avoid adding -p on all clone commands.

@DrumRobot
Copy link

By setting the pushInsteadOf option in git config, you can automatically redirect git push commands that target a specific URL to an alternative URL. This option is particularly useful when you want to convert HTTPS URLs to SSH URLs, allowing you to push through SSH instead of HTTPS.

For example, with the following configuration:

[url "ssh://[email protected]/"]
    pushInsteadOf = https://github.com/

Git will attempt to push to ssh://[email protected]/ whenever you push to a URL starting with https://github.com/. This means:

  • Even if you clone the repository using HTTPS, Git will push using SSH.
  • You can thus use SSH authentication for git push without manually adjusting the URL.

With this setup, you no longer need to use commands like ghq -p. The ghq -p option normally forces ghq to clone a repository with an ssh URL instead of https, but by configuring pushInsteadOf, you can clone with HTTPS and still push with SSH automatically.

@cgimenes
Copy link
Author

By setting the pushInsteadOf option in git config, you can automatically redirect git push commands that target a specific URL to an alternative URL. This option is particularly useful when you want to convert HTTPS URLs to SSH URLs, allowing you to push through SSH instead of HTTPS.

For example, with the following configuration:

[url "ssh://[email protected]/"]
    pushInsteadOf = https://github.com/

Git will attempt to push to ssh://[email protected]/ whenever you push to a URL starting with https://github.com/. This means:

  • Even if you clone the repository using HTTPS, Git will push using SSH.
  • You can thus use SSH authentication for git push without manually adjusting the URL.

With this setup, you no longer need to use commands like ghq -p. The ghq -p option normally forces ghq to clone a repository with an ssh URL instead of https, but by configuring pushInsteadOf, you can clone with HTTPS and still push with SSH automatically.

Thanks, but my main point is cloning through SSH; I don't want to authenticate using HTTPS. And, using your solution I would have to authenticate through both protocols while working on private repos.

@DrumRobot
Copy link

HTTPS authentication entirely, you can configure Git to always use SSH for both cloning and pushing. This ensures that all Git operations use SSH, preventing the need for HTTPS authentication on private repositories. Here’s how to do it.

Solution: Set insteadOf to Force SSH for All GitHub Operations

Using the insteadOf option, you can tell Git to automatically replace https://github.com/ with ssh://[email protected]/ for both cloning and pushing.

Add both insteadOf and pushInsteadOf configuration:

git config --global url."ssh://[email protected]/".insteadOf "https://github.com/"
git config --global url."ssh://[email protected]/".pushInsteadOf "https://github.com/"

The changed configuration:

[url "ssh://[email protected]/"]
    insteadOf = https://github.com/
    pushInsteadOf = https://github.com/

This setting tells Git to use the SSH URL for any GitHub operation originally specified with an HTTPS URL, so:

  • You can clone, pull, and push without HTTPS authentication.
  • No need for ghq -p or HTTPS credentials; everything goes through SSH.

Now, anytime you run a command with an HTTPS GitHub URL, Git will automatically substitute it with the SSH URL instead. This way, you can manage all GitHub repositories via SSH authentication only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants