Lesson 9 of 60 – Creating a Git Repository
15%

Creating a Git Repository

A Git repository is a project directory that Git tracks for version control. You can create a new repository inside an existing project folder using the git init command.

Note: The git init command initializes a Git repository in the current directory. It does not automatically create a commit or upload your project to GitHub.

1. What is a Git Repository?

A Git repository is a directory that contains your project files and Git's version control information.

The repository allows Git to track changes and maintain project history.

2. What Does git init Do?

The git init command creates the Git metadata needed to start tracking a project.

git init

After running the command, the current directory becomes a Git repository.

3. Creating a Project Folder

First, create a project directory.

mkdir my-project

This creates a folder named my-project.

4. Entering the Project Folder

Use the cd command to enter the project directory.

cd my-project

Git commands such as git init will then operate in this directory.

5. Initializing the Repository

Run the following command inside your project folder:

git init

Git initializes the current directory as a repository.

6. The .git Directory

When you run git init, Git creates a hidden directory named .git.

This directory contains Git's repository metadata and is essential to the repository.

7. Why is .git Important?

The .git directory stores information that Git uses to manage the repository, including objects, references and configuration.

Deleting this directory removes the repository metadata from that project directory.

8. Checking Repository Status

After initializing a repository, use:

git status

This displays the current state of the repository.

9. Example Git Repository

my-project/
    .git/
    index.html
    style.css
    script.js

The .git directory contains Git's internal repository information while the other files are project files.

10. Creating a Repository in an Existing Project

You do not have to create a new project folder before using Git. You can initialize Git inside an existing project.

cd my-existing-project
git init

Git will begin managing the project as a repository.

11. Git Does Not Automatically Track Everything

Running git init does not automatically create commits for all project files.

You need to stage changes and create commits:

git add .
git commit -m "Initial commit"

12. Checking Untracked Files

Create a file after initializing the repository:

touch index.html
git status

Git will normally show the new file as an untracked file.

13. Staging a File

Use git add to stage a file for the next commit.

git add index.html

The file is now included in the staging area.

14. Staging All Changes

To stage all changes in the current directory and its subdirectories, you can use:

git add .

Review the staged changes with git status before committing.

15. Creating the First Commit

After staging your project files, create your first commit:

git commit -m "Initial commit"

This records the staged changes in the repository history.

16. Viewing the First Commit

You can view the repository history using:

git log

The output includes information about the commit, such as its identifier, author, date and message.

17. Initializing a Repository with a Branch Name

You can specify the initial branch name while initializing a repository. For example:

git init -b main

This initializes the repository with main as its initial branch name.

18. Initializing in the Current Directory

The simplest form of the command initializes the current directory:

git init

Make sure you are inside the intended project directory before running the command.

19. Initializing a Repository in a Specific Directory

Git can also initialize a repository in a specified directory:

git init my-project

This creates or initializes the repository in the specified project directory.

20. Git Repository and GitHub

A local Git repository does not automatically connect to GitHub. You can later add a GitHub repository as a remote.

git remote add origin https://github.com/username/my-project.git

The remote connection will be covered in a later lesson.

21. Repository Without GitHub

A Git repository can work entirely on your local computer. You can create commits, branches and inspect history without connecting the repository to GitHub.

git init
git add .
git commit -m "Initial commit"
git log

22. Checking the Repository Root

Git can show the root directory of the current working tree with:

git rev-parse --show-toplevel

This can be useful when working with nested directories.

23. Git Repository Workflow

Create Project
      ↓
Enter Project Folder
      ↓
git init
      ↓
Create or Modify Files
      ↓
git status
      ↓
git add .
      ↓
git commit

This is a basic local Git workflow.

24. Creating a Web Project Repository

mkdir website
cd website

touch index.html
touch style.css

git init

git status

This creates a simple website project and initializes it as a Git repository.

25. Adding Web Project Files

git add index.html
git add style.css

You can stage files individually when you want precise control over what will be included in the next commit.

26. Making the Initial Commit

git commit -m "Create website structure"

This creates a commit containing the staged website files.

Afterward, use git log to inspect the commit history.

27. Common git init Mistakes

  • Running git init in the wrong folder
  • Forgetting to check the current directory
  • Assuming git init creates a commit
  • Assuming git init uploads files to GitHub
  • Deleting the .git directory accidentally
  • Ignoring untracked files before the first commit

28. Useful Repository Commands

git init
git status
git add .
git commit -m "Initial commit"
git log
git branch

These commands provide a simple starting point for working with a newly initialized repository.

29. Complete Repository Example

mkdir student-project
cd student-project

touch index.html

git init

git status

git add index.html

git commit -m "Initial student project"

git log

This example creates a project, initializes Git, stages a file, creates a commit and displays the history.

30. Summary of git init

The git init command is used to create a new Git repository. It creates the .git directory that contains Git's repository information.

After initialization, you can add files, create commits, create branches and eventually connect the repository to a remote service such as GitHub.

git init
git status
git add .
git commit -m "Initial commit"

In the next lesson, we will learn about the structure of a Git repository and understand the role of the .git directory.

📌 Key Points

  • git init initializes a Git repository.
  • A Git repository contains project files and Git metadata.
  • Git creates a hidden .git directory during initialization.
  • git init does not create a commit automatically.
  • git add stages files for a commit.
  • git commit records staged changes.
  • git status displays the repository state.
  • git log displays commit history.
  • A local repository does not automatically connect to GitHub.
  • A repository can later be connected to a remote repository.

🧠 Quick Quiz

Question: Which command is used to initialize a new Git repository?