Skip to Content
Lesson 2

Basic Git Commands and Repositories

Workplace Context

You have been tasked with building a new feature for your company’s website. To ensure that your work is tracked and can be easily collaborated on by your team, you need to use Git for version control.

In this training, you will learn how to use basic Git commands to manage and track changes in your project. You will create a repository, stage changes, and commit them. Once you have completed the training and understand these fundamental commands, you will be better equipped to manage your code efficiently and work seamlessly with your team.


Learning Objectives

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

  • Use Git commands to initialize a repository, track, and commit changes.
  • Explain how to stage and commit files to the repository.
  • Demonstrate how to view the history of changes using git log.
  • Revert changes using git reset and git checkout.

Initializing a Git Repository

Once you have created your project and set up your GitHub account, you can start using Git to track your changes.

Step-by-Step: Initializing a Repository

  1. Navigate to Your Project Folder: Use the cd command to navigate to the directory where your project files are located.

    $ cd path/to/your/project
  2. Initialize the Repository: Run the following command to initialize Git in your project directory. This will create a .git folder, which is where Git will track all the changes in your project.

    $ git init
  3. Check Git Status: Use the git status command to see the state of your repository. It will show you any untracked files or changes that haven’t been committed yet.

    $ git status

Staging and Committing Changes

What Are Staging and Committing?

  • Staging: When you modify files, Git does not automatically start tracking those changes. You need to stage the files that you want to include in the next commit.
  • Committing: After staging the files, you create a commit, which is a snapshot of your project at that point in time. Commits allow you to go back and see the history of your project and revert to previous versions if needed.

Think about staging and committing like shopping. Before you begin shopping, you create a list of items you would like to purchase. Once you arrive at the store, you put those items into your cart, the “staging” area. You have not committed to buying the items yet. You can always put them back on the shelf and commit to them later if you wanted to; they are still on your list, after all.

Once you arrive at the checkout, you commit to purchasing the items by buying them. Once you’ve committed, they’re a part of your life (or in the case of development, the project). However, thanks to the magic of modern shopping (version control), you can always return the items at a later date (reverting your commits).

Step-by-Step: Staging and Committing Changes

  1. Create or Modify a File: Create or modify a file in your project. For example, let’s create an index.html file and add some basic content:

    <html> <head> <title>My Project</title> </head> <body> <h1>Hello, world!</h1> </body> </html>
  2. Stage the File: Use the git add command to stage the file. You can stage individual files or stage all changes at once.

    $ git add index.html

    To stage all files at once, use:

    $ git add .
  3. Commit the Changes: Once the file is staged, commit the changes with a descriptive message. The commit message helps others (and yourself) understand what changes were made.

    $ git commit -m "Added basic structure to index.html"

Viewing the Commit History with git log

After making multiple commits, you may want to review the history of changes in your project. The git log command allows you to see all the commits made, who made them, and when they were made.

Step-by-Step: Using git log

  1. View the Commit History: Run the git log command to see a list of all the commits in your repository.

    $ git log

    The output will show each commit’s hash (a unique identifier), the author, the date, and the commit message.

    Example output:

    commit 2c4f4b8b9c4e5a7b896f1f94e9381bf3624fdf43 Author: Your Name <your.email@example.com> Date: Wed Sep 4 14:33:00 2024 +0100 Added basic structure to index.html

Reverting Changes with git reset and git checkout

Sometimes, you may want to undo changes or go back to a previous version of your project. Git provides commands to help you do this safely.

Step-by-Step: Reverting Changes

  1. Unstage Changes: If you have staged changes but decide you do not want to include them in the commit, you can unstage them using:

    $ git reset index.html
  2. Revert to a Previous Commit: If you need to roll back your project to a previous commit, you can use git checkout:

    $ git checkout <commit-hash>

    This command will switch your project to an earlier version, but be careful—this can make changes that are not committed yet disappear. Make sure your work is saved before reverting!


Examples from Industry

React Repository

How do you view these kinds of changes inside a repository on GitHub? Let us take another look at the React repository on GitHub to uncover some of the commits that have occured there.

On the main page of the repository, you will see the latest commits, its identifier, how long ago the commit was, and the total number of commits:

The image shows the GitHub repository for the React project. It highlights several key details: The branch being viewed is main. The repository has 291 branches and 143 tags. The latest commit is by the user acdlite, with the message "Start prerendering Suspense retries immediately" (#30934). The commit hash is d6cb4e7, and the commit was made 39 minutes ago. The repository has a total of 19,417 commits. Below the commit message, a list of folders in the repository (.codesandbox, .github, compiler, etc.) is visible, along with their last updated details.

Since React is in constant development, the current state of the repository is likely to look quite different.

If you click on the latest commit, it will take you to the commit page which shows the commit description, the number of changes files, additions, and deletions, and a visual representation of those changes within the file system. Take a look through the commit we’ve linked here, as well as the latest commit, and familiarize yourself with the layout of GitHub’s interface.

At this stage, it is not important to understand the code itself!


Knowledge Check

Which Git command stages changes for commit?

  • Select an answer to view feedback.

What does the git log command do?

  • Select an answer to view feedback.

Summary

In this lesson, you learned how to use Git to initialize a repository, stage files, commit changes, and view the history of your project using git log. These fundamental commands allow you to keep track of your work, manage different versions of your project, and collaborate with your team.

Next, we will explore how to push your changes to a remote repository like GitHub and collaborate with others.


References


Additional Resources


This lesson now follows the updated guidelines and ensures learners understand how to manage a Git repository and handle changes efficiently. Let me know if you’d like to make any further adjustments!