Setting up a Git server on Windows can be a useful way to manage your code repositories and collaborate with others on a local network. Whether you’re working on a small project with a team or just want to keep your code organized, hosting your own Git server provides more control and flexibility compared to using a third-party service like GitHub or GitLab. In this tutorial, we’ll walk through the steps to set up a Git server on Windows and configure it for local development.
Prerequisites
Before we begin, make sure you have the following installed on your Windows machine:
Git for Windows (includes Git Bash and Git GUI)
A text editor (e.g., Notepad , Visual Studio Code)
A web browser
You’ll also need to have administrative privileges on your Windows machine to complete the setup process.
Step 1: Create a Bare Repository
First, we need to create a bare repository that will serve as the central repository for your project. Open Git Bash and navigate to the directory where you want to create the repository:
bash
cd /path/to/repository
Then, use the following command to create a bare repository:
bash
git init –bare myproject.git
This will create a new directory called `myproject.git` that contains the Git repository files without a working directory.
Step 2: Create Windows Users for Git Clients
Next, create Windows user accounts for each person who will be accessing the Git server. These users will need to have read and write permissions to the repository directory.
To create a new user, open the Windows Control Panel and navigate to “User Accounts”. Click “Add or remove user accounts” and then “Add a new user in PC settings”. Follow the prompts to create a new user account.
Once you have created the necessary user accounts, grant them access to the repository directory by setting the appropriate permissions. Right-click on the `myproject.git` directory, select “Properties”, and then go to the “Security” tab. Click “Edit” and then “Add” to add the new user accounts. Select the user accounts and check the “Allow” box for the “Full control” permission.
Step 3: Configure Git SSH Server
To allow remote access to the Git server, we need to set up an SSH server. Git for Windows includes a version of OpenSSH, so we can use that to configure the SSH server.
Open Git Bash and run the following command to set the system environment variable for the SSH server to pick up the Git commands:
bash
$gitPath = Join-Path -Path $env:ProgramFiles -ChildPath “git\mingw64\bin”
$machinePath = [Environment]::GetEnvironmentVariable(‘Path’, ‘MACHINE’)
[Environment]::SetEnvironmentVariable(‘Path’, “$gitPath;$machinePath”, ‘Machine’)
Then, restart the SSH server for the changes to take effect:
bash
Restart-Service sshd
If you haven’t already, generate SSH keys for each user who will be accessing the Git server. They can do this by running the following command in Git Bash:
bash
ssh-keygen.exe -t ed25519 -f ~/.ssh/id_ed25519
The public key will be located at `~/.ssh/id_ed25519.pub`. Have each user copy their public key to the `C:\Users\{user}\.ssh\authorized_keys` file on the Git server machine.
Step 4: Clone the Repository
Now that the Git server is set up, users can clone the repository to their local machines. In Git Bash, navigate to the directory where you want to clone the repository and run the following command:
bash
git clone ssh://{username}@{servermachine}:/path/to/myproject.git
Replace `{username}` with the Windows username, `{servermachine}` with the hostname or IP address of the Git server machine, and `/path/to/myproject.git` with the actual path to the repository.
If you encounter an error related to the default shell, you can work around it by setting PowerShell as the default shell in the Windows registry or by launching PowerShell to run the Git commands:
bash
git config –local remote.origin.uploadpack “powershell git-upload-pack”
git config –local remote.origin.receivepack “powershell git-receive-pack”
Once the repository is cloned, users can start working on their local copies and push changes back to the server using standard Git commands like `git add`, `git commit`, and `git push`.
Step 5: Manage the Git Server
To manage the Git server, you can use the Git Bash command line interface or a graphical tool like Git GUI. Some common management tasks include:
Creating new repositories: Use the git init –bare command to create a new bare repository.
Updating repository permissions: Modify the directory permissions for the repository to grant or revoke access to users.
Backing up repositories: Regularly back up the repository directories to ensure data integrity.
Monitoring server activity: Check the SSH server logs for any errors or unauthorized access attempts.
By following these steps, you should have a fully functional Git server running on your Windows machine, ready for local development and collaboration. Remember to keep your server secure by regularly updating the operating system and SSH server software, and by monitoring for any suspicious activity.