TIL: Migrating a Git Repo to Use SSH
Today I learned how to migrate and existing git repo to use SSH Keys for extra security.
I wanted to add an extra layer of security to one of my accounts by using SSH keys. I had previously done this with my accounts at work, where all I had to do was just drop my public key in my account and everything seemed to just work for me. Great.
So I tried to do the same thing on my personal account and found that when I went to push, I was getting 403 responses back. So obviously the security was working, but I wasn’t sure how to fix it.
Initially I started to google around for “How to include ssh key with git push”, but this just took me in a bunch of circles. Lots of talk about adding new keys and creating ssh config files to manage multiple keys and blah blah.
I eventually found out that what I was looking for was changing the remote url of my existing repository. Because I had initially created the repo without security and cloned it over https, every time I attempted to push I was doing it over https, and the call was failing out.
In order to fix this, I needed to use the
git remote set-url <source> <repo> command, as detailed in the link above. This will update your local repo to swap from the defaulted https over to using ssh. It will then start using your ssh keys.
You can check to see which version you’re using by issuing the
git remote -v command inside of the repository. This will show you exactly what the source and url are of your existing repository. If you see something like
https://github.com/USERNAME/REPOSITORY.git, you know it’s using https. If you see something like
firstname.lastname@example.org:USERNAME/REPOSITORY.git, you know it’s using ssh.
After making this change, you can start looking into more advanced stuff like having multiple different keys for different repos or sites, but I’m good with what I’ve got for now.
Hopefully this can help save someone else some time when they’re looking to get their existing git repo to start using their ssh key.