Code has been added to clipboard!

How to Create and Manage Your First Git Repository

Reading time 7 min
Published Jan 16, 2018
Updated Oct 3, 2019

Wherever you work, whether on your local computer or cloud, the files and changes must be saved somewhere. That is the reason why Git repository is one of the basic and most popular functions to use. Git repository, just like the name indicates, serves as a storage for all the changes and files that relate to a certain project. That is the shortest answer to the question of what is a repository.

Initializing Git repository is quite simple. However, if you want to learn how to work with Git repository properly, you must understand how and why files are stored there. Some Git repositories can be local, placed directly at your local computer. You can use an already existing directory as a repository for your Git files or create a brand new one.

If you work in a team or you are invited to make changes to a particular code, chances are you will need to access a remote Git repository. Git clone command or Git clone repository are the names for a command that create a local version for you of that remote Git repository so that you could create your own changes without any damage to the remote version.

In this tutorial, you will be explained how to clone a Git repository and how to initiate one from the very beginning.

Keep reading below!

Git Repository: Main Tips

  • Git has a place called repository, sometimes shorten as repo, where your Git projects are stored.
  • All changes in a project and versions of saved files are in its repository.
  • Git clone command is used to create an identical copy of remote Git repository, but it can also be placed locally, on developer's computer .

The Basics of Repository

What is a repository? To put it simply, it is a place where all the files of a certain project are placed. It can be both, either a remote storage space or a locally accessible directory. For instance, storage space in the cloud, an online host or local folder on your computer all can serve as repositories. In this particular folder, sometimes also abbreviated as a repo, Git saves all the files and project-related information, such as changes history.

There are two types of repositories - local and remote. Correspondingly, there are two options to work with Git: to start an entirely new project or join an already existing one.

How to Access Git Repository

One of the easiest ways to get a Git repository is to use a local directory. It must be free, i.e., not yet used for a version control system work.

Most of the work with Git is done locally. However, it is important to keep in mind that none of the files are tracked until a user asks Git to start doing so - that will make the files version-controlled. Initialising a repository is exactly a way to ask Git to enable version control on the files held in that repository.

How to Initialize Git Repository

To begin with Git repository, you have to go to the project's that you intend to control directory. Here you have to run 'git init' command. This process is called initializing a repository.

$ git init

This command creates a new Git repository. In general, it is used to initialize an empty new repository or convert an existing unversioned project into a Git repo.

You need a folder in which you will hold all the files of your project. One way to do it is simply manually. First of all, choose a location where you want to create a folder. In this example you can see how the "Git lessons" folder is created on the desktop:

That is done by typing this command into a new terminal window:

$ cd Desktop/Git lessons/
$ git init

Another way to do the same is by right-clicking your mouse, opening a Git bash and typing Git init.

You should see a message similar to this one with Git lesson folder created on Desktop:

Initialized empty Git repository in .../Desktop/Git lessons/.git/

Creating Git Repository in an Already Existing Directory

Instead of creating a brand new folder for your Git repository, you can overtake an already existing directory. How to initiate changes to that directory depends on which operating system you are using. In this particular example a directory is created here: user/my_new_project.

If you are using Windows, type:

$ cd c/user/my_new_project

For Mac OS:

$ user/username/my_new_projectcd

For Linux:

$ cd /home//user/my_new_project

You have indicated which directory you want to use. Now type in this command to initiate Git repository:

$ git init

With the last command line, you make a new Git repository existing. Its subdirectory .git will have all the repository files. No changes are tracked yet, though - that will be covered in the other tutorial.

Important: If you want to start a version-control directory which was not empty before, first you need to start to track it and only then do an initial commit.

Adding Files to Git Repository

Adding more files to the Git repository you have created might be quite useful.

In order to do that you have to create a new Git repo with the command, you have just learned: $git init. After that you have to execute two other commands: add and commit. The full code should look like the following:
$ git init
$ git add --all
$ git commit -m 'this message shows I have initiated a commit'

Note: It is important to leave messages when saving your files.

Getting to Know Your Git Repository

It is useful to know where you are while moving around the directories. You can check your location by typing this command into the terminal or Git Bash:

$ pwd

The result should be something similar to this location, depending on where your folders have been saved:

User/Desktop/Git lessons

It is also essential to be able to check the current status of your repository. Running the following command in a newly created repository should provide you with similar answers from Git:

$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

Cloning Remote Git Repository

Git clone repository is another useful command that developers often use at Git. It is quite common that you work on a shared project and the repository of it is placed somewhere remote. In this case to contribute to the code you might need to get your own clone of the repository. You will be explained here how to clone a Git repository to your local computer.

In order to clone Git repository you have to use git clone [url] command. For this example, a linkable Git repository is at GitHub. If you want to clone the linkable library, navigate to the directory where you want to have the copied Git repository and run this command with your folders names:

$ git clone https://github.com/folder/folder

Git Repository: Summary

  • Git system stores and saves the files to a directory called repository, also abbreviated as repo.
  • Git repository is used to save the information about the changes in the project.
  • Repository can be created locally, or it can be a clone Git repository which is a copy of a remote Git repo.
Learn Git
Introduction
Installation
Setup
Branch
Repository Basics
Server
Workflow Cheat Sheet
Save Changes
Move Files
Undo Changes
Examine Repository