Creating and Initializing a Git Repository

Tutorial 3 of 5

Creating and Initializing a Git Repository

1. Introduction

Overview

In this tutorial, we will walk through the process of creating and initializing your first Git repository. A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed.

Learning Goals

By the end of this tutorial, you will be able to:

  • Install Git
  • Create a new Git repository
  • Initialize your repository
  • Make your first commit

Prerequisites

You should have a basic understanding of using the terminal or command line.

2. Step-by-Step Guide

Installing Git

Before you can create a Git repository, you need to have Git installed on your computer. If you have not installed Git, you can download it from the official Git website.

Creating a Git Repository

Once Git is installed, you can create a new repository. Open your terminal, navigate to the folder where you want your project to reside, and then type git init. This will create a new Git repository in your current folder.

$ cd /path/to/your/folder
$ git init

Making Your First Commit

Now you can start tracking changes. Create a new file in the repository folder, add some code, and then save the file. After saving, you can add the file to your repository using the git add command. This tells Git to include the changes in the next commit.

$ echo "print('Hello, World!')" > hello.py
$ git add hello.py

After adding the file, you can commit your changes with git commit. This saves your changes to the repository.

$ git commit -m "My first commit"

3. Code Examples

Example 1: Initializing a Git repository

This code snippet shows how to initialize a Git repository.

$ cd /path/to/your/folder  # navigate to your project folder
$ git init  # initialize the Git repository

Example 2: Making a commit

This code snippet shows how to create a file, add it to the repository, and make a commit.

$ echo "print('Hello, World!')" > hello.py  # create a new Python file
$ git add hello.py  # add the file to the repository
$ git commit -m "My first commit"  # commit the changes

4. Summary

In this tutorial, we've learned how to install Git, create a Git repository, and make a commit. With these skills, you can start tracking changes to your projects and collaborate with others.

5. Practice Exercises

Now it's time to practice what you've learned. Try these exercises:

  1. Create a new Git repository in a different folder.
  2. In this new repository, create a file called goodbye.py. In this file, write a Python script that prints "Goodbye, World!".
  3. Add goodbye.py to the repository and make a commit with the message "Goodbye commit".

Here are the solutions:

$ cd /path/to/your/other/folder  # navigate to another project folder
$ git init  # initialize another Git repository
$ echo "print('Goodbye, World!')" > goodbye.py  # create a new Python file
$ git add goodbye.py  # add the file to the repository
$ git commit -m "Goodbye commit"  # commit the changes

Keep practicing these steps until you're comfortable with the process. Happy coding!