Module 2 Lesson 1: Initializing a repository
·DevOps

Module 2 Lesson 1: Initializing a repository

Turn any folder on your computer into a Git repository. Learn when and how to use 'git init' to start tracking your project's history.

Module 2 Lesson 1: Initializing a repository

The first step in any Git project is getting Git to actually "watch" your files. There are two ways to get a Git repository on your machine: you can create one from scratch, or you can copy an existing one. In this lesson, we focus on the first method: Initialization.


1. What is git init?

The git init command is the "big bang" of a Git project. It creates a new sub-folder named .git in your current directory. This hidden folder contains all of the internal Git metadata—the history, the config, and the "brains"—of your repository.

When to use it:

  • You have an existing project folder that isn't under version control yet.
  • You are starting a brand new project and want to track it from day one.

2. Practical Steps

Let's say you have a folder called my-cool-app. To start tracking it, you would follow these steps:

# 1. Navigate into your project folder
cd my-cool-app

# 2. Run the initialization command
git init

What happens next?

You will see a message like: Initialized empty Git repository in /Users/yourname/my-cool-app/.git/

From this moment forward, Git is "standing by." It hasn't saved any of your files yet, but it knows the folder exists and is waiting for your further instructions (which we'll learn in Module 3).


3. Creating a Repo with a Default Branch

In Module 1, we set our global default branch to main. If you didn't do that, Git might initialize your repo with a default branch called master (the older standard).

If you want to specify the branch name during the very first command, you can use:

git init -b main
graph TD
    NormalFolder["Normal Folder (Files only)"] -- "git init" --> GitRepo["Git Repository (Files + .git folder)"]
    GitRepo --> Tracking["Git is now monitoring changes"]

4. Important Warning

Do Not Initialize a Repo Inside a Repo! You should only run git init at the "root" of your project. If you have a project folder inside another project folder, and you run git init in both, you'll end up with a "nested repository" which can cause major confusion and errors.

Always check if a .git folder already exists before initializing. On Mac/Linux, use ls -a to see hidden files. On Windows, enable "Show hidden files" in File Explorer.


Lesson Exercise

Goal: Initialize your first repository.

  1. Create a new folder on your Desktop called git-practice.
  2. Open your terminal or Git Bash.
  3. Navigate into that folder using cd.
  4. Run git init.
  5. Check if the .git folder was created.
    • Mac/Linux: ls -d .git
    • Windows: dir /a:h

Observation: You'll see that the folder looks empty to the naked eye, but the invisible .git folder is now the foundation of your version control time machine.


Summary

In this lesson, we established:

  • git init turns a regular folder into a Git repository.
  • It creates a hidden .git folder that stores all project history.
  • You should only initialize Git at the root of a project, never inside an existing Git repo.

Next Lesson: We’ll look at the second way to get a repository: Cloning an existing project from the cloud.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn