Basic Git Commands: git init, git add, and git commit

Git is a distributed version control system that allows developers to track code changes, collaborate with others, and manage their code base effectively. Whether you're new to Git or just want a deeper understanding of its core commands, this post will cover three of the most fundamental in Git.

Basic Git Commands: git init, git add, and git commit
Photo by Yancy Min / Unsplash

Git is a distributed version control system that allows developers to track code changes, collaborate with others, and manage their code base effectively. Whether you're new to Git or just want a deeper understanding of its core commands, this post will cover three of the most fundamental commands in Git: git init, git add, and git commit.

By the end of this guide, you will have a solid understanding of how to initialize a Git repository, stage files for tracking, and commit changes to the repository.

1. What is Git and Why Use It?

Before diving into the commands, let’s briefly discuss why Git is important and how it helps developers. Git is a powerful tool for:

  • Tracking Changes: Git keeps a history of every change you make to your code, so you can easily roll back if something breaks.
  • Collaboration: Git allows multiple developers to work on the same project simultaneously without overwriting each other's changes.
  • Branching and Merging: Git makes it easy to create branches for new features or fixes, allowing for isolated changes that can later be merged into the main project.

When working with Git, you primarily interact with repositories (repos). There are two main types of repositories:

  • Local Repository: A repository on your computer where you can track your code changes.
  • Remote Repository: A repository hosted on a server (e.g., GitHub, GitLab) that you can push your local changes to and collaborate with others.

Now, let’s dive into the essential commands that make Git so powerful.

2. Git Basics: Working with Local and Remote Repositories

The general workflow in Git revolves around two types of repositories:

  1. Local repository: A Git repo on your local machine.
  2. Remote repository: A shared Git repo hosted on a server (e.g., GitHub or GitLab).

The basic flow usually involves:

  • Initializing a local Git repository with git init.
  • Adding files to the staging area using git add.
  • Committing changes with git commit.

To push these changes to a remote repository, you’ll eventually use commands like git push (not covered in this post), but first, let's focus on local operations with the basics.

3. git init: Initializing a Git Repository

3.1 Creating a New Git Repository

The first step when starting any new project in Git is to initialize a Git repository. The command git init is used to turn any directory into a Git repository. This repository will track your project’s history of changes.

Steps:

  1. Open your terminal or command prompt.
  2. Navigate to the folder where your project is located (or create a new folder).
  3. Run git init to initialize the repository.

Example:

# Create a new project directory
mkdir my-git-project

# Navigate into the directory
cd my-git-project

# Initialize a Git repository
git init

After running git init, Git will create a hidden .git directory inside your project folder. This .git folder contains all the information Git needs to track your project (e.g., commits, branches, etc.).

Output:

Initialized empty Git repository in /path/to/my-git-project/.git/

At this point, your folder is now a Git repository. However, it’s empty, and no files are being tracked yet. This brings us to the next step: staging files.

3.2 Cloning an Existing Repository

If you’re working on an existing project, you can clone a remote repository instead of initializing one locally. This is useful when collaborating on open-source projects or working with teams.

Example:

git clone https://github.com/dolpa/playground.git

The command will download a copy of the repository from GitHub (or any other remote hosting service) and set up a local Git environment for you.

4. git add: Staging Files for Commit

4.1 What is Staging in Git?

Git tracks changes in your project through two areas:

  • Working Directory: The actual files in your project folder.
  • Staging Area: Files that are marked (or "staged") for inclusion in the next commit.

You can think of the staging area as a preparation space where you choose which files to include in the next snapshot of your project. Once files are staged, they can be committed to the repository.

4.2 Using git add to Stage Files

The command git add is used to move files from your working directory to the staging area. You can choose to stage individual files, multiple files, or even all files at once.

Example 1: Stage a single file

git add filename.txt

Example 2: Stage multiple specific files

git add file1.txt file2.txt

Example 3: Stage all files in the project

You can stage all the changes (including new, modified, and deleted files) using the . shorthand:

git add .

You can check which files are staged by running git status. This command shows you the current state of your working directory and staging area.

Example:

git status

Output:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   README.md

The files listed under "Changes to be committed" are staged and ready to be committed.

5. git commit: Committing Changes

5.1 What is a Commit in Git?

A commit in Git is a snapshot of your project at a particular point in time. When you commit changes, you’re telling Git to record the current state of the staged files. Each commit in Git has a unique identifier (hash) and includes a message describing the changes.

Think of a commit like a "save point" in your project’s history. If anything goes wrong, you can revert to any previous commit.

5.2 Creating a Commit with git commit

Once your files are staged, the next step is to create a commit. The basic syntax for committing is:

git commit -m "Your commit message here"

The -m flag is used to provide a message that describes what changes were made in the commit. This message is crucial for understanding the project’s history.

Example:

git commit -m "Added README file and initial project setup"

Output:

[master (root-commit) d1e2f3g] Added README file and initial project setup
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

Now, the changes have been saved in the repository, and Git will track them as part of the project’s history.

6. Putting It All Together: A Complete Workflow

Here’s a typical workflow that involves initializing a Git repository, adding files to the staging area, and committing them:

# Step 1: Create a new project folder
mkdir my-new-project
cd my-new-project

# Step 2: Initialize Git in the folder
git init

# Step 3: Create a new file
echo "My Project" > README.md

# Step 4: Stage the file for commit
git add README.md

# Step 5: Commit the file to the repository
git commit -m "Initial commit with README file"

At this point, you’ve successfully set up a Git repository, tracked a file, and committed it to your project’s history.

Conclusion

By mastering the basic Git commands—git init, git add, and git commit—you now have the tools to start tracking your project’s changes effectively. These commands form the foundation of working with Git, whether you’re managing a personal project or collaborating with others on a team.

Here’s a quick summary of the commands covered:

  • git init: Initializes a new Git repository.
  • git add: Stages files to be included in the next commit.
  • git commit: Commits the staged changes to the repository with a descriptive message.

In future posts, we’ll explore more advanced Git features, such as branching, merging, and collaborating on remote repositories. Stay tuned!

Read next

Setting Up Your First Git Repository

In the world of development, Git is an essential tool for managing code, collaborating with others, and maintaining a history of changes in your projects. In this post, we’ll guide you through setting up your first Git repository from scratch.

Installing Git on Different Operating Systems

This guide covers the installation of Git on Windows, macOS, and Linux, with detailed steps for each operating system. Learn how to set up Git quickly using Git Bash, Homebrew, and package managers. Perfect for developers and DevOps professionals getting started with Git.