How to change a connection to GitHub from SSH to HTTPS?

GitGithubSshHttpsGit Remote

Git Problem Overview


I created my first repository in GitHub yesterday. When making the connection I used SSH instead of HTTPS, so I went through a little painful SSH key creation and connection process. At some point I got stuck and the connection failed. I wondered at that moment how I could revert the process I started and begin with a HTTPS connection instead. Happily, today I got the connection working through SSH but I'm wondering about the value of being able to change the type of connection (SSH vs HTTPS) and if there is a way to do it.

Git Solutions


Solution 1 - Git

Assuming your remote is called origin, run

  • git remote set-url origin https://...
  • git remote set-url --push origin https://...

You can view the configured remotes with git remote -v, which should now show your updated URLs.

See the documentation for git-remote for more details.

Solution 2 - Git

here are some aliases (oneliners) to switch your repo from ssh to https and back. Assuming your default remote is named origin and your remote is github.com

alias git-https="git remote set-url origin https://github.com/$(git remote get-url origin | sed 's/https:\/\/github.com\///' | sed 's/[email protected]://')"
alias git-ssh="  git remote set-url origin [email protected]:$(    git remote get-url origin | sed 's/https:\/\/github.com\///' | sed 's/[email protected]://')"

they're a bit longer than necessary to make them idempotent

Solution 3 - Git

Put these alias definitions in your ~/.bashrc:

alias git-ssh='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^https://([^/]*)/(.*)$,git@\1:\2,'\'')"'

alias git-https='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^git@([^:]*):/*(.*)$,https://\1/\2,'\'')"'

Then,

  • to switch from https to ssh: git-ssh
  • to switch from ssh to https: git-https

Successfully tested with both github.com and gitlab.com repos.

Note: I used -E for extended regular expression, and I used comma, instead of the usual slash, to separate the parts of the substitution operation. Together, these helped reduce leaning toothpick syndrome.

Solution 4 - Git

So guys I struggled with this problem for a while but finally got how to solve it. First make sure you have updated your git to the latest version using:

C:\> git update-git-for-windows

Afterwards run the command:

C:\>git config --global url."https://github.com/".insteadOf [email protected]:

Then:

C:\>git config --global url."https://".insteadOf git://

If you still get the Permission denied (publickey) error you can do the process manually as follows:

Navigate to your .gitconfig file.

You can check for its location using:

git config --list --show-origin

Open the file using notepad.

Delete this section:

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

Replace with:

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

After this you should be able to log in using your Personal Access token successfully.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondickbarbaView Question on Stackoverflow
Solution 1 - GitChrisView Answer on Stackoverflow
Solution 2 - GitHarry MorenoView Answer on Stackoverflow
Solution 3 - GitRobin A. MeadeView Answer on Stackoverflow
Solution 4 - GitVitechView Answer on Stackoverflow