Monday, February 24, 2020

Git Server on Windows with OpenSSH, Remote and Local Repository

1. What is Git Server?
It is a server installed Git service. In which, Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
In this article, I will guide you step by step to setup a Git Server on Windows with OpenSSH.

2. What is Remote & Local Repository?
Remote Repository is a repository on a server where source codes from developers are centralized. While Local Repository is a repository cloned (copied) from Remote Repository to developer's computer (client).
The following picture will illustrate how repositories working.


3. Required tools
+Win32 OpenSSH
+Git for Windows

4. Install Git for Windows
Go to Git for Windows, select & download suitable version for your Windows (32-bit or 64-bit).
Installing it with option "Use Git and optional Unix tools from the Command Prompt" and "Use the OpenSSL library".



After installing, open Windows cmd or Git bash run below command to check if it is installed ok:
git --version
5. Install OpenSSH
Go to Win32 OpenSSH, select & download a suitable version for your Windows. Unpack it to a folder (e.g. C:\OpenSSH).
Run Windows PowerShell as Administrator right, change to the OpenSSH folder, then run below command to install:
powershell -ExecutionPolicy ByPass -File install-sshd.ps1
See the following picture for more details:


Open Windows Services then set OpenSSH SSH Server & OpenSSH Authetication Agent to Automatic  and start them.

Because OpenSSH use port 22 by default, so you must open this port on your Windows firewall. You can change the SSH service port value (e.g. Port 1235) in its config file: C:\OpenSSH\sshd_config_default. If you change the port, remember open the firewall for new port and restart SSH services.

To check if the OpenSSH server is working, on a client computer download PuTTY tool and connect to the SSH server via its IP and a Windows user on the server.
In case your client computer already had ssh client, you can use Windows cmd to connect to the server. If your client doesn't have, you can install Win32 OpenSSH on the client. See below picture for sample of checking SSH connection.

Every time you connect to the SSH server, it requires to input password. To avoid password, you can use Public & Private keys for authenticating. Let generate these keys on your clients by using ssh-keygen tool, for example:
ssh-keygen -t rsa -b 4096
It will generate 2 files: id_rsa and id_rsa.pub in SSH folder of Windows user on the client (C:\Users\<username>\.ssh). Remember let passphrase as empty (key enter) when it asks you key in, it will help you skip to enter passphrase every time you connect to the server. Copy id_rsa.pub file to the SSH folder of Windows user on the server (C:\Users\<username>\.ssh) and rename it to authorized_keys. Then on the server, right click on this file and make sure removing rights of all users except Administrators and SYSTEM, for example:


On the server, verify if file C:\ProgramData\ssh\sshd_config (file config of SSH service) has below lines uncommented:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
If not, let uncomment them then restart OpenSSH SSH Server. Try again with ssh username@computername_or_IP, it will login to SSH Server without entering any password/passphrase.

6. Create Remote Repository (central repo)
On the server, for existing source folder, you can run below commands from Windows cmd:
cd D:\mygit\my_central_repo
git init --bare
Or create new central repo by command:
git clone --bare D:\mygit\my_central_repo
On the client, create a folder and add remote repository into:
cd E:\local_repo
git init
git remote add origin hunglv@192.168.10.101:D:/mygit/my_centro_repo
In which origin is a name standfor remote repo. Next you must run 2 below work arround commands to set powershell as default Shell in registry:
git config --local remote.origin.uploadpack "powershell git-upload-pack"
git config --local remote.origin.receivepack "powershell git-receive-pack"
Now you can fetch remote repo to your local repo for working:
git fetch origin
OK, you have done the setup for Git Server on Windows with OpenSSH, Remote and Local Repository. Next is common Git commands that are often used.

7. Basic Git commands
git fetch <remote name> <branch>: fetching repo versioning data from remote repo to local repo
git pull <remote name> <brannch>: get all (included new files & merge updated files) from remote repo to local
git add <file>: add new file to local repo
git commit -m <"message">: commit all updates / news to your local repo
git push <remote> <branch>: push all updates / news from your local repo to remote repo
git remote -v: see remote repo linked
git status: check status / changes in local repo

The end for this article. Hope you can start your projects on Git easily.
Any comment is welcome. Bye!

2 comments:

  1. Hi there it was a very useful information.
    As you mentioned I have installed the git in server machine and client.
    I have followed the steps above and I facing an issue with fetching.
    It is throwing a fatal error: could not read from remote repo.
    could you help me with this?
    Thank you.

    ReplyDelete

Subscribe to RSS Feed Follow me on Twitter!