Initializing a Git Repository

In Git, a repository is the structure unit where whole project’s files and their modifications history are stored. The first thing to do when using Git for version control is initializing a repository. In this lesson we will explain what is a Git repository, differences between both local and remote repositories, and also how can we create one from scratch.

What is Git Repositories?

A Git repository is essential concept in Git. Repositories is storage spaces to store your projects. And it's where Git tracks the history of changes made to the files. There are two main types of repositories:

  1. 1
    Local Repository: Stored on your computer and used for personal version control. (As a Local Server)
  2. 2
    Remote Repository: Stored on an online server e.g., GitHub (Which is most famous platform) and used for collaboration and backup.

When you create a repository, Git starts monitoring what you changes in your project directory, enabling you to use all its powerful version control features.

Creating a Git Repository

You can create your first Git repository by following these next steps:

  1. 1
    Open Your Terminal or Command Prompt
  2. 2
    Run the git init Command
  3. 3
    Verify Repository Creation

Before you can create a repository, navigate to the directory of your project in your terminal or command prompt. For example, if your project is in a folder named my-project, you can reach your folder by using the following command: cd path/to/my-project

Once inside your project directory, initialize your new repository by running git init This command creates a hidden folder called .git inside your project directory. The .git folder contains all the information Git needs to track and save changes in the project.

To ensure that Git has created the repository, run git status If the creation of the Git repository succeed, Git will show a message indicating that you are on the default branch (often can be main or master) and that meaning there are no commits yet.

A New Project With a New Repository

  1. 1
    First we should create our new project folder mkdir my-project then enter the new directory cd my-project
  2. 2
    Creating the repository in the new folder git init
  3. 3
    Add a new file to your project echo "Hello, Git!" > README.md
  4. 4
    Lastly, check the repository status git status You’ll see a message telling you that README.md is an untracked file.

Notes on `git init`

  1. 1
    Avoid Nested Repositories: Do not try to create a Git repository inside another Git repository. This can lead to confusion and potential errors. So don`t try this at all.
  2. 2
    Re-Initializing a Repository: Running git init command in an existing repository will not overwrite your data but may update configuration files if necessary.
  3. 3
    Cloning vs. Initializing: Use git clone is an easy method to duplicate an existing repository instead of initializing a new one.

Conclusion

Initializing a Git repository is the first step to start using Git for version control. Once the repository is set up, Git begins tracking changes, allowing you to take full advantage of its features. In the next lesson, we’ll explore the Basic Git Workflow, which covers the steps to manage changes in your project effectively.