117

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.

5
  • 1
    If you want to git push local modifications to github, 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. Commented Jun 6, 2015 at 13:52
  • 2
    @BasileStarynkevitch, both SSH and HTTPS connections can be used to push to GitHub (and many other hosts). Commented Jun 8, 2015 at 21:57
  • 1
    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. Commented Sep 15, 2018 at 19:18
  • 1
    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. Commented Sep 15, 2018 at 19:20
  • 1
    After reading answers, if you want to change the connection of a submodule: How to change the remote repository for a git submodule? Commented Feb 5, 2022 at 17:12

5 Answers 5

181

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The elided ... make this somewhat difficult to understand. This answer seems clearer: we're removing the [email protected]: prefix and replacing it with the https://github.com/ prefix, keeping the <Username>/<RepoName> postfix (which should be the same URL as from the green clone code button on the GitHub repo).
22

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

3 Comments

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/')
..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).
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:/')
6

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.

Comments

5

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.

Comments

5
  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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.