How to Create a Local Repo on Rocky Linux 9

How to Create a Local Repo on Rocky Linux 9

Are you looking for a tutorial on how to create a local repo on Rocky Linux 9? Then this guide is for you.

A local repository is hosted locally on your machine instead of a server on the internet. By having a local repository, you can easily tweak your source code, collaborate with others, and maintain different versions of your project. 

In this guide, we’ll show you how you can create and set up a local repo on your Rocky Linux device. 

Let’s get started!

How to Create a Local Repo on Rocky Linux 9

For this tutorial, we will use our terminal instead of GUI clients for Git. Before anything else, you should update your system since you will be required to install multiple tools. You may update your system using this command:

$ sudo dnf update

Output:

update rocky linux

When the update is done, we can move on to installing the necessary tools. First, we will install Git. Git is a version control system. It’s a popular tool used by programmers and developers to create snapshots of their projects.

Install Git with this command:

$ sudo dnf install git

Output:

install git rocky linux

After installing Git, you may check its version to confirm if the installation was successful. You can check its version using this command:

$ git --version

Output:

check git version

As you can see from the screenshot, the current version of Git installed is 2.39.3, which means the installation was successful.

Now we can start working on creating our local repository.

Initialize a Directory as Git Repository

Let’s create a new directory that will act as the storage for all the files we will keep in the repository. For this tutorial, we will name it ‘test-repo’. You can rename it any way you want. A good naming convention to follow for repos is using hyphens between each word in a multi-word name.

Create the directory with this command:

$ mkdir test-repo

Then use the ls command to see if the new directory was created, like this:

How to Create a Local Repo on Rocky Linux 9

After that, navigate into the newly created directory with this command:

$ cd test-repo

You can check which directory you’re in with this command:

$ pwd

Output:

cd command

We are currently in the directory we just created, as expected.

The next thing to do is to initialize the directory as a Git repository. To do that, run this command:

$ git init

Output:

How to Create a Local Repo on Rocky Linux 9

If you see a similar message, that means you successfully created an empty Git repository. This command is used for creating empty Git repos or reinitializing an existing repo. Suppose you haven’t created a directory for initializing. In that case, you can use that as an argument to the above command, like this:

$ git init myrepo

This command will create a directory called ‘myrepo’ and automatically initialize it as a Git repo.

The initial branch name for this repo will be ‘master’ unless you decide to change it. You can rename the branch using this command:

$ git branch -m <name>

Additionally, if you want a different initial branch name for all your repositories, issue this command:

$ git config --global init.defaultBranch <name>

Add files to Staging

Now that you’ve initialized your repository, you can start storing some files in it. After all, that’s what we’re going for here. We’ve made some dummy documents for that purpose. You choose which files you would like to keep here. Bring the files to this repo. You can do that by copying using this command:

$ cp -r ~/Templates/* ~/test-repo

Output:

copy files to local repo

With this command, we are copying everything inside the Templates directory(including sub-directories) and taking them to our test-repo. To confirm the copy operation was successful, we can check the current contents of test-repo with the help of this command:

$ ls

Output:

ls command

Seeing that we have some content, that means the copying worked. Now it’s time to add these files to staging. You can add all the files in the current repository to the index with this command:

$ git add .

Output:

How to Create a Local Repo on Rocky Linux 9

Having the period sign ‘.’ at the end means you are adding all content to the index. You can check the added files using this command:

$ git status

Output:

git status

As expected, all 3 of our files are currently there. If you want to undo a git add command, you use this command:

$ git rm --cached <file>

Now if you try to check the status of the index, you will see that the files were removed and yet to be added.

Output:

undo git add

Another way to do this is to use the following command:

$ git reset

If you want to add only one or multiple files instead of all, you can write the name of each file after the git add command, like this:

$ git add doc1.txt doc2.txt

Now if we check the status, only these two files will be in the index.

Output:

git add files

Commit Your Changes

After adding the files, you need to make a commit. In a commit, you document the last changes you made in the files with a short message. So in our example, suppose we made a change in doc2.txt file. In our commit, we will record this change with a suitable message. The basic syntax is as follows:

$ git commit -m “Commit message”

You use the -m flag and then wrap your commit message with double quotes. So an example commit would look like this:

$ git commit -m “Add files to repo”

Output:

How to Create a Local Repo on Rocky Linux 9

The output shows that your commit was successful. For an unsuccessful commit, you would receive error messages. If you try to see the status now, this is what you will get:

git status

Just like git add, you can undo a commit. For that, run the following command:

$ git reset --soft HEAD~

You can also add a number as an argument for how many commits you would like to undo. So if you would like to undo the last 3 commits, the command will look like this:

$ git reset --soft HEAD~3

After you commit your changes to your local repo, it’s now ready to be uploaded to different project hosting websites such as GitHub, GitLab, BitBucket, etc.

Final Thoughts

This guide shows you how to create a local repo on Rocky Linux 9. We showed you the step-by-step process with simple examples. So you can follow it to create your own local repo in no time.

If this guide helped you, please share it.

Related Posts