How to create a local Git repository on Rocky Linux with SSH
Git is one of the most widely used version control systems on the planet. With this tool, you can store and sync your code and even collaborate with your team members. Many devs use third-party, git-based social coding platforms like GitHub or GitLab, but anyone can run git locally without uploading any code to the internet.
There can be a dozen reasons why version control should stay within your own LAN. Here are just a few:
-
Small home LAN: Small development projects for home automation software and version-controlled configuration files
-
Educational institutions: Manage course-related coding projects that allow students to collaborate on campus
-
R&D labs: Handle proprietary or experimental projects with total security and control
-
Game development: Working with large binary files and assets that can be cumbersome to manage over cloud-based version control systems
-
Large corporations: Security policies may restrict the use of cloud-based services, so a local git repo (even with 25,000 global users) may fit the bill
Setting up git is very easy, thanks to modern tooling and mature services like SSH. In fact, if you follow this how-to guide, you’ll be up and running in just a few minutes.
What you'll need to set up a local Git repo
To get up and running with a local Git repo requires only one machine, but we’re going to use two so you can get a sense of how Git works in a collaborative environment.
All you'll need are a Rocky Linux machine to host the repository and another machine with an SSH client for accessing the repository. If you want to configure a dedicated user to own your Git repository, you’ll also need sudo privileges.
Step one: Install Git
First install Git on both your Rocky Linux server and client machine. (Git runs on every popular operating system.) To install Git on Rocky Linux, use this command:
sudo dnf install git -y
To make sure it installed correctly, run:
git --version
With Git ready on both machines, it's time to create an SSH key so you can securely communicate between the two machines.
Step two: Create a Git user on the server
On the Rocky Linux machine, create a user to own and set up a Git repository. Let’s create a user named git-user
:
sudo adduser git-user
Step three: Create your SSH key
SSH is a secure way to connect to other computers. It has encrypted communication, remote command execution, and other secure network services. Setting up SSH keys is a way of establishing trust between two machines so you can work without typing in a password every time you run Git commands.
On the client machine, check to see if there’s an existing SSH key (use ls -la ~/.ssh
and see if there’s a file called id_rsa
). If you don't already have an SSH key created, do so with the command:
ssh-keygen
Answer the questions and make sure to create a strong/unique passphrase for the key. You should see output like the following:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa
Your public key has been saved in /home/username/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:f9u6kuHP9fJXkzDTqk4p+GGmpkwyXuQEiAEjFdr8wXs username@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|*.o. |
|o*.. |
|o.o.o . |
| ..o + . |
| ooE S = .|
| +. ... .. o.|
| o + . *.=o . o|
| . * .= B+ +...|
| . oo. ..+*o.o+|
+----[SHA256]-----+
When the key generation is complete, copy the public key to the Rocky Linux server with the command:
ssh-copy-id git-user@SERVER
Where SERVER is the IP address of the Rocky Linux server.
Step four: Create your first repository
You can now create your first repository.
Create a parent folder to house your repositories with the command:
mkdir -p git
Change into the new test project folder with:
cd git
A bare Git repository contains only the version control data (the contents of the .git directory) and lacks a working directory, making it ideal for sharing changes as a server because it does not maintain a checked-out copy of the source files. In contrast, a non-bare repository includes a working directory with a checked-out copy of the source files, allowing direct file editing and is typically used for active development on a local machine.
Initialize the bare repository with:
git init --bare TEST_PROJECT.git
You can now proceed to clone this repository on your local machine to start working with it, ensuring that all interactions conform to best practices for server-based repositories.
Step five: Test your repository
Go back to your client machine and clone the new repository with the command:
git clone git-user@SERVER:~/git/TEST_PROJECT.git
This command clones the bare repository from your Git server. If no commits have been pushed to the repository yet, you will see the following message:
Warning: You appear to have cloned an empty repository.
To add code to the repository, clone it to your local machine, add your files, commit them, and then push the changes back to the server. This will populate the repository, and subsequent clones will include the added content. Here’s a series of instructions you can run on your client machine for validation:
Command | What it does |
echo “# Test Project Title” > README.md |
Adds a Markdown-formatted text headline to a file called README.md. |
git add README.md |
Tells git to add README.md to the list of files it watches for your next commit in your local repo. |
git commit -m “Add initial README file” |
Stages and commits the README.md file to the repo. We’re also leaving a comment describing the purpose of the commit. |
git push origin master |
Syncs the local repo changes to the server’s git repo. Now, anyone cloning the repository after this executes will receive the repo with the README file included. |
Congratulations, you've just created your first private Git repository. At this point, you might want to check out some Git tutorials on the web to learn more about how it works and what best practices to follow.
To allow others on your network to clone and interact with this repository, they should generate an SSH key pair on their machine and send the public key to you. You can then add this key to the authorized_keys file of the Git user on the server. This setup enhances security by using key-based authentication instead of passwords. Just remember, each user needs to set up their SSH key pair and have their public key authorized on the server.