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 Answers
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.
1 Comment
... 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).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
/ in their path: git remote set-url origin $(git remote get-url origin | sed 's/^git@\(.*\):\/*\(.*\).git/https:\/\/\1\/\2.git/')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).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:/')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
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
httpstossh:git-ssh - to switch from
sshtohttps: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
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)
git pushlocal 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.pushto GitHub (and many other hosts).git remote set-urlI typically text-edit the.git/configfile. You just need to observe different url structure for both on some repo servers.