dickbarba
dickbarba

Reputation: 1131

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

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.

Upvotes: 112

Views: 96991

Answers (5)

Moran77
Moran77

Reputation: 149

  git remote -v
# View existing remotes
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

    git remote set-url origin https://github.com/user/repo2.git

# Change the 'origin' remote's URL

    git remote -v

# Verify new remote URL
# origin  https://github.com/user/repo2.git (fetch)
# origin  https://github.com/user/repo2.git (push)

Upvotes: 4

Vitech
Vitech

Reputation: 61

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.

Upvotes: 6

Robin A. Meade
Robin A. Meade

Reputation: 2474

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.

Upvotes: 5

Harry Moreno
Harry Moreno

Reputation: 11603

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

Upvotes: 22

Chris
Chris

Reputation: 136968

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.

Upvotes: 176

Related Questions