Over the last weeks I had a lot of problems with Git credentials in combination with Azure DevOps. Whenever I worked with a repository, my Git client asked for username and password. It got so annoying that I started to switch to SSH authentication and since then that problem never showed up again.
I focus on Azure DevOps in this post, but GitHub offers the same features in a slightly different location.
Register your public key
Tools like Git Kraken fully support SSH authentication and create the key pair for you. All you need to do is to copy the public key to the clipboard and then add that to your account on Azure DevOps. You find the key management in the user settings at the right top corner:
Checkout using SSH key authentication
On the clone repository dialog, you can switch between HTTPS and SSH. If you select SSH you get a different URL to your repository:
As you can see, Azure DevOps offers you a direct access to your SSH key settings from this dialog as well.
This URL gets into your Git client to clone a repository the same way as you would use a HTTPS URL.
Switch checked out repositories to SSH
As I described in an earlier post, changing a remote URL in a repository is a simple task. The SSH URL is just another remote address that you can configure.
Open your repository in the Command Prompt (cmd) and check the current remote URL:
1 2 3 |
$ git remote -v origin https://***.visualstudio.com/DefaultCollection/_git/PROJ (fetch) origin https://***.visualstudio.com/DefaultCollection/_git/ PROJ (push) |
Now set the new remote URL for SSH:
1 |
$ git remote set-url origin ***@vs-ssh.visualstudio.com:v3/***/PROJ/PROJ |
Your new URL should now be active:
1 2 3 |
$ git remote -v origin ***@vs-ssh.visualstudio.com:v3/***/PROJ/PROJ (fetch) origin ***@vs-ssh.visualstudio.com:v3/***/PROJ/PROJ (push) |
Limitations
SSH as a replacement for access tokens or username & password only works with Git clients that support SSH. Unfortunately, this is currently not the case with GitHub Desktop.
Visual Studio supports SSH, but it falls back to the Git tools and may ask you for credentials you do not have. You can configure Git tools for SSH, or you keep the remote URL for origin on HTTPS and add another remote for SSH. It is a little bit more work, but you gain a lot of flexibility.
Conclusion
When your Git client supports SSH, then it is a lot simpler to switch to SSH as constantly fight with your credentials. And should that not work for you, you simply switch the origin back to HTTPS.