HOW TO REMOVE HTTPS REMOTE ORIGIN AND ADD SSH IN GIT

How to Remove HTTPS Remote Origin and Add SSH in Git

How to Remove HTTPS Remote Origin and Add SSH in Git

If your Git repository is currently using an HTTPS remote URL and you want to switch to SSH (for easier authentication with SSH keys), follow these steps:

1. Check Current Remote URL

First, verify your existing remote URL:

git remote -v

Example output:

origin  https://github.com/username/repo.git (fetch)
origin  https://github.com/username/repo.git (push)

2. Remove the HTTPS Remote

Remove the existing origin remote:

git remote remove origin

(Alternatively, you can rename it instead of deleting it: git remote rename origin old-origin)

3. Add the New SSH Remote URL

Now, add the SSH version of your GitHub repository URL:

git remote add origin git@github.com:username/repo.git

(Replace username/repo.git with your actual GitHub username and repository name.)

4. Verify the New Remote

Check that the new SSH remote is set correctly:

git remote -v

Expected output:

origin  git@github.com:username/repo.git (fetch)
origin  git@github.com:username/repo.git (push)

5. Test SSH Connection (Optional)

Ensure your SSH key is properly set up with GitHub:

ssh -T git@github.com

If successful, you'll see:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

6. Push Changes to the New Remote (If Needed)

If you have local changes, push them to the new SSH remote:

git push -u origin main

(Replace main with your branch name if different.)

Why Switch from HTTPS to SSH?

  1. No password prompts (uses SSH keys)
  2. More secure (key-based authentication)
  3. Easier for automation (CI/CD pipelines, scripts)

Troubleshooting

If SSH key is not set up:

  1. Generate a new SSH key: bash ssh-keygen -t ed25519 -C "your_email@example.com"
  2. Add it to ssh-agent: bash eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
  3. Add the public key to GitHub: bash cat ~/.ssh/id_ed25519.pub

Then paste it in GitHub → Settings → SSH and GPG keys.

If git push fails:

  1. Ensure you have write permissions to the repo.
  2. Try: bash git push --set-upstream origin main
×