If you want to use Git on Windows then you should definitly use Git for Windows - it even provides a native Windows bash and some.NIX tools. If you really want a full Linux environment on Windows, then go to the Microsoft Store and install one of them (Ubuntu, SUSE, Debian, Kali). Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance.
Introduction
Developers are usually more used to Unix based system to set up all environments, but there are times we have to use Windows. If this is the case, setting up Git on windows is a must. This article briefly reviews all the steps from Git installation to SSH set up. Hope you find this article helpful.
Overview
How to install Git on Windows
How to set up SSH keys for Git
Things need to be careful with
1. Install Git on Windows
First download Git for Windows on the official website: git-scm.com/downloads
Run the downloaded file and do the setup.
On the “Select Components” page, we can leave it as is, or you can change upon your own preference.
On the “Adjusting your PATH environment” page, we want to select the first option: “Use Git from Git Bash only“, unless you really want to use Windows Command Prompt. Git Bash feels like a unix-like terminal and it is capable to do most of the work we need.
On the “Configuration the line ending conversions” page, you can select based on your need. Again, I would recommend the third option: “Checkout as-is, commit as-is“, as this is the safest option for our code.
Next page, we can just select the first option: “Use MinTTY“.
On the “Configuring extra options” and “Configuring experimental options” page, leave as it or select on your need.
Now we can just hit the Install button and finish the installation.
Once it is installed, we can open the Git Bash and give it a try. Type git command.
2. Set up SSH Keys
Now that we got Git running, it is time to set up SSH keys for Git, so that we don’t need to input password every time. It is more convenient but also safer.
The concept is we create a public/private key pair; put the public key to the remote server, and keep the private key on your local machine. The server can authenticate the client if the client has the corresponding private key.
First we need to generate key pair. Type this command on Git Bash:
ssh-keygen -t rsa
For now, we can just press Enter to use default key name and empty passphrase.
‘id_rsa’ is the private key, and ‘id_rsa.pub’ is the public key.
Git Bash Windows Change Drive
Next we want to put the public key to the remote server. Different servers have different requirements of how to publish your public key. For GitHub, we can just follow user setting -> SSH Keys page and copy the public key there.
For the private key, the location and key name doesn’t really matter; we can config the Git to use the correct path of private key, see here. However, if the key path is ~/.ssh/id_rsa, then it should work out-of-the-box without any configuration.
To verify SSH is working for our Git, we can try the following command on Git Bash:
If everything works well, then the prompt would say “You’ve successfully authenticated“.
Git Bash Windows Add Ssh Key
Last thing, to use SSH login, the remote urls of repositories need to be SSH type, instead of HTTPS type. See here to change it.
3. Things need to be careful with
We do want to keep the private key in a safe place, and set the permission to be only readable to yourself: -r-------- (mode 400).
If you need SSH access for multiple git users, I found this post is very helpful. Basically you need to add the username as part of the host name of the repository, and then add the same hostname and corresponding private key file in the config file ~/.ssh/config.
Let me also mention Cygwin (ignore this if you don’t have Cygwin installed). It happened on my laptop that Git and Cygwin were not compatible with each other and I couldn’t use Git as normal.
End
Thanks for your reading. Please leave comments if you have any questions.