24/7/365 Support

Creating a local repository with Git CLI in Ubuntu server

Now that we have the Git binaries installed, let's take a step forward and create our first local Git repository.

Getting ready

Make sure that you have installed Git.

How to do it…

We will take a common path by starting a new pet project, where we will simply create a new local directory, add some files to it, and then realize, Ohh I am gonna need a version control system:

So, yes, quickly create your new project:

$ mkdir mynewproject

$ touch mynewproject /index.html

$ touch mynewproject /main.js

$ touch mynewproject/main.css

Add some sample content to these files by editing them:

Now you need to create a Git repository for this project. Sure, Git covered you with the git init command.

Make sure you are in the project directory and then initialize a new repository, as follows:

$ cd mynewproject

$ git init

This will initialize a new empty repository under the project directory. A new hidden directory gets created with the name .git. This directory will contain all the metadata of your Git repository and all revisions of every single file tracked by Git.

How it works…

Here, we have used the git init command to initialize a new repository on our local system. The files created before initializing a repo are optional; you can always skip that step and directly use git init to create a new local repository. Later, when you need to push (synchronize) this repo with a remote hosted repository, you can simply use the git remote add command. We will see examples of git remote add in the next recipes.

With the git init command, you can also create a bare repository by using the --bare flag. The difference between a normal repository and a bare repository is that a bare repository does not have a working copy. You cannot use a bare repository directly to edit and commit files. Unlike a normal repository, where revision history, tags, and head information is stored in a separate .git directory, a bare repo stores all this data in the same directory. It is meant to be a central shared repository where multiple people can commit their changes. You need to clone these types of repositories to access and edit files. The changes can be pushed using the git push command from the cloned copy.

There's more…

You can also use git clone to clone existing repositories. The repository can be local or remote. The clone command will replicate the contents of a parent repository, including revision history and other details. We will see more details of git clone in the next recipes.

See also

You can read more by following these links:

Git init: https://git-scm.com/docs/git-in it

Git clone: https://git-scm.com /docs/git-clone

Help Category:

What Our Clients Say