2) Standard Git Workflow Lesson

Git Init

7 min to complete · By CodingNomads Team

In this lesson, you will practice using the git init command so that you can track your projects with Git.

What is Git Init?

The git init command initializes a new Git repository on your local machine. Here's what it looks like in action:

$ git init

Running this command creates a new Git repository in the directory you are currently in.

Colorful illustration of a light bulb

Info: You can use git init to initialize a Git repository either in an existing project, or in a new and empty project folder on your computer.

Initialize An Empty Git Repository

Say you're starting a project, and even though you have no files or folders yet, you'd like to practice version control right from the git-go, as it were. ;)

In order to track your project with Git from the beginning, you can initialize an empty Git repository by using the git init command. Practice initializing an empty Git repository by following the instructions below.

Colorful illustration of a light bulb

Note: Remember that you do not want to initialize a new git repository within another git repository. Always use git init in folders that are not part of any other Git repository.

1. Make a Directory

In your terminal, run the following commands:

$ cd ~/Documents
$ mkdir My_Amazing_Project
$ cd My_Amazing_Project

2. Initialize Git

Next, run git init to initialize your Git Repository:

$ git init

After running the commands, you should receive terminal output that looks similar to this:

Initialized empty Git repository in /Users/ryandesmond/Documents/My_Amazing_Project/.git/

What just happened? The above commands:

  • Make an empty directory you want to put under version control
  • Navigate to that directory
  • Initialize it as a Git-tracked repository

As the output tells you, the command created a new .git sub-directory in your current working directory ~/Documents/My_Amazing_Project/.

3. Check Directory

You can see it and inspect it further by running the command ls -al on UNIX, or dir on a Windows system.

The .git folder contains the "database" that tracks all the changes and branches of your files within this directory, as well as all its subdirectories.

Colorful illustration of a light bulb

Info: git init also creates a new default branch, and adds your entire project to that branch. You will learn more about what this means in a little while.

Initialize An Existing Project With Git

If you have an existing code project on your computer and you want to put this project under Git version control, you can initialize this project as a Git project.

For this part of the exercise, you'll first make a new demo directory as you did before. This time, you'll also populate it with a few example files, and for the sake of example, you'll treat it as if it were an existing project folder.

1. Make an "Existing" Project Folder

Run the following commands from the CLI to set up your demo directory:

$ cd ~/Documents
$ mkdir My_Amazing_project_2
$ cd My_Amazing_project_2

2. Populate Directory

Running these commands to populate the directory with a few empty files:

$ touch example1.txt
$ touch example2.txt
$ touch example3.txt
Colorful illustration of a light bulb

Note: Each instance of the touch command in this example simply creates a new empty .txt file within your current directory.

3. Initialize Git

Now run git init to initialize your project as a Git repository:

$ git init

Your terminal output should look similar to this:

Initialized empty Git repository in /Users/ryandesmond/Documents/My_Amazing_project_2/.git/

The above code snippets do a few things:

  • Make an empty directory you want to put under version control
  • Navigate to that directory
  • Populate it with files
  • Initialize it as a Git-tracked repository

4. Check Directory

Run ls -al to check it out. The output confirms that the .git folder has been created.

Colorful illustration of a light bulb

Info: Remember that the .git folder is a hidden folder, so it will only show up if you explicitly list also the hidden files, e.g., with ls -al in bash.

Summary: Git Init

The git init command initializes a new Git repository on your local machine. In this lesson, you practiced using the git init command to:

  • Initialize an Empty Git Repository
  • Initialize an Existing Project With Git

With this you've practiced creating a repository in Git, and initialized it. Git is now set up to track any changes that happen within it. In the next lesson, you'll learn more about how Git tracks changes.