Computer Science Grade 9 20 min

4. Basic Git Commands: init, add, commit

Learn the basic Git commands for initializing a repository, adding files, and committing changes.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Initialize a new Git repository in a project folder using `git init`. Explain the purpose of the staging area in Git's workflow. Add new or modified files to the staging area using `git add`. Commit staged changes to the local repository with a descriptive message using `git commit -m`. Describe the three-state model of Git: the working directory, the staging area, and the repository. Use the `git status` command to check the current state of their project files. Ever wished you could save your coding project at different checkpoints, like a video game save point? 🎮 That's exactly what we're going to learn how to do! This lesson introduces the three most fundamental commands in Git: `init`, `add`, and `commit`. You will learn how to start...
2

Key Concepts & Vocabulary

TermDefinitionExample Repository (Repo)A digital folder where Git stores all the files, history, and versions of your project. Think of it as the project's dedicated history book.After running `git init` in a folder called `my-game`, that folder becomes a Git repository. Working DirectoryThe regular folder on your computer containing your project files that you are currently editing.The `my-game` folder on your desktop where you are editing `player.py` and `enemy.js`. Staging Area (or Index)A temporary holding area where you place the specific changes you want to include in your next 'save point' (commit). It's like putting items in a shopping cart before you check out.After changing `player.py`, you run `git add player.py` to move it to the staging area, ready for the...
3

Core Syntax & Patterns

Initialize a Repository git init Run this command once inside your main project folder. It creates a hidden `.git` subfolder that turns your project into a Git repository, allowing Git to start tracking it. Stage Files for a Commit git add <filename> OR git add . Use `git add <filename>` to add a specific file to the staging area. Use `git add .` as a shortcut to stage all new and modified files in the current folder. Commit Staged Files git commit -m "Your descriptive message here" Use this command to save the files from the staging area into the repository's history. The `-m` flag allows you to write a required commit message directly on the command line.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
In your `space-adventure` project, you create a new file `ship.py` and modify the existing `game.py`. You want to create two separate, logical commits: one for the new ship functionality and one for the game modifications. What is the correct sequence of commands?
A.git add . -> git commit -m 'Add ship' -> git commit -m 'Modify game'
B.git add ship.py -> git commit -m 'Add ship' -> git add game.py -> git commit -m 'Modify game'
C.git add ship.py game.py -> git commit -m 'Add ship and modify game'
D.git commit -m 'Add ship' -> git add ship.py -> git commit -m 'Modify game' -> git add game.py
Challenging
A student has a project with `main.py` and `utils.py`. Both are tracked by Git. They modify `main.py` and create a new file `tests.py`. They then run the following commands: 1. `git add main.py` 2. `git commit -m "Refactor main logic"` After these commands, what is the status of `tests.py` according to `git status`?
A.It is staged for the next commit.
B.It is part of the 'Refactor main logic' commit.
C.It is an untracked file.
D.It is listed as 'modified' but not staged.
Challenging
Using the tutorial's analogy of the Staging Area as a 'shopping cart' and the Working Directory as the 'store shelves', what action is most analogous to `git commit`?
A.Going to the checkout counter, paying for the items, and getting a permanent receipt.
B.Putting an item from the shelf into your shopping cart.
C.Walking around the store and looking at different items on the shelves.
D.Deciding to put an item from your cart back on the shelf.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from III. Introduction to Version Control with Git

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.