89

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
dickbarba
  • 901
  • 1
  • 6
  • 5
  • If you want to `git push` local modifications to [github](http://github.com/), you'll better keep the *ssh* connection. Read some ssh tutorial, and configure the private & public keys to avoid typing your password more than once. – Basile Starynkevitch Jun 06 '15 at 13:52
  • 1
    @BasileStarynkevitch, both SSH and HTTPS connections can be used to `push` to GitHub (and many other hosts). – ChrisGPT was on strike Jun 08 '15 at 21:57
  • Instead of `git remote set-url` I typically text-edit the `.git/config` file. You just need to observe different url structure for both on some repo servers. – eckes Sep 15 '18 at 19:18
  • I often use https as fetch url and ssh as Push url, the advantage is that I don’t need to unlock my ssh key for random fetches. – eckes Sep 15 '18 at 19:20
  • After reading answers, if you want to change the connection of a submodule: [How to change the remote repository for a git submodule?](https://stackoverflow.com/q/913701) – li ki Feb 05 '22 at 17:12

5 Answers5

145

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    @dragonfly02, nope. If it's asking for an SSH passphrase it's still using SSH. Check your remotes by running `git remote -v`, as described in the answer. – ChrisGPT was on strike Oct 12 '22 at 11:21
16

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/git@github.com://')"
alias git-ssh="  git remote set-url origin git@github.com:$(    git remote get-url origin | sed 's/https:\/\/github.com\///' | sed 's/git@github.com://')"

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

Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
  • 10
    Thanks for that. Here's a version that works for non-github.com and URLs that have, or don't have, a leading `/` in their path: `git remote set-url origin $(git remote get-url origin | sed 's/^git@\(.*\):\/*\(.*\).git/https:\/\/\1\/\2.git/')` – Tim Bunce Feb 08 '19 at 10:35
  • 1
    ..and the reverse of @TimBunce's version: `git remote set-url origin $(git remote get-url origin | sed 's/^https:\/\/\([^\/]*\)\/\(.*\).git/git@\1\:\2.git/')` (assuming the result should NOT have a leading `/` in its path). – Arjan Sep 10 '21 at 16:09
  • For AWS code commit URLs (e.g. `https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/somerepo`), you can just switch the https: and ssh: `git remote set-url origin $(git remote get-url origin | sed 's/^ssh:/https:/')` or to switch to SSH `git remote set-url origin $(git remote get-url origin | sed 's/^https:/ssh:/')` – phhu Jan 27 '22 at 22:27
4

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 git@github.com:

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 = git@github.com:"]
    insteadOf = https://github.com/

Replace with:

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

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

Vitech
  • 41
  • 3
2

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.

Robin A. Meade
  • 1,946
  • 18
  • 17
1
  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)
Moran77
  • 119
  • 8