Skip to content

Instantly share code, notes, and snippets.

@asksven
Created April 2, 2016 20:48
Show Gist options
  • Select an option

  • Save asksven/b37e8d83eca7f77484be9dd7af2b98e6 to your computer and use it in GitHub Desktop.

Select an option

Save asksven/b37e8d83eca7f77484be9dd7af2b98e6 to your computer and use it in GitHub Desktop.
If 2fa is enabled on github switch to ssh instead of https on linux
1. generate an ssh keypair on your linux box
ssh-keygen -t {rsa|dsa}
2. add the public key to github: profile - settings - ssh keys
3. switch from https to ssh
Check your repo remote:
git remote -v
should show:
origin https://github.com/USERNAME/REPOSITORY.git (fetch)
origin https://github.com/USERNAME/REPOSITORY.git (push)
Change the remote:
git remote set-url origin git@github.com:USERNAME/OTHERREPOSITORY.git
verify:
git remote -v
should show:
origin git@github.com:USERNAME/OTHERREPOSITORY.git (fetch)
origin git@github.com:USERNAME/OTHERREPOSITORY.git (push)
@isaac-souza
Copy link
Copy Markdown

Thanks a lot man, just what I was looking for

@NickyGuants
Copy link
Copy Markdown

Hi, thanks for this. How can I configure Github to always use the ssh URL rather than https for all repositories I create?

@asksven
Copy link
Copy Markdown
Author

asksven commented Sep 3, 2021

Gitlab decides on the protocol based on the protocol in the URI or the repo. This can be changed after the fact by changing the remote URI using "git remote" (-v to list them)

@ArmandoShala
Copy link
Copy Markdown

Worked like a charm

@sbodvanski
Copy link
Copy Markdown

Thank you!

@vmari
Copy link
Copy Markdown

vmari commented Sep 6, 2022

When changing origin is not an option we can use this config:

git config --global url."ssh://git@github.com".insteadOf "https://github.com"

@lostcarpark
Copy link
Copy Markdown

Very useful, thanks!

@vdawg-git
Copy link
Copy Markdown

Thank you!

@HelenGuohx
Copy link
Copy Markdown

Thank you, it works

@aster-hu
Copy link
Copy Markdown

Thanks! Works like a charm and it helps me resolve the error "fatal: could not read Username for 'https://github.com': Device not configured"

@xiaohaoo
Copy link
Copy Markdown

xiaohaoo commented Feb 1, 2024

Is there a more elegant way?

@sherry-channel-capital
Copy link
Copy Markdown

sherry-channel-capital commented Feb 21, 2024

@xiaohaoo It's not that complicated, just switch the URL with a simple command or git clone the SSH URI if it's a new setup. No need to complicate it, what more elegance would you expect?

@phisad
Copy link
Copy Markdown

phisad commented Mar 27, 2024

Amazing! Thanks!

@codernirdesh
Copy link
Copy Markdown

When changing origin is not an option we can use this config:

git config --global url."ssh://git@github.com".insteadOf "https://github.com"

This got helpful for me. Thank you. ❤️

@lucas-pcs
Copy link
Copy Markdown

Useful ! thanks !

@alexbodn
Copy link
Copy Markdown

thank you very much my friend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment