← Git & GitHub from Zero
Git basics: git init, status, add, commit and log

Lesson 1 of 7

Git basics: git init, status, add, commit and log

Learn the five Git commands you will use every day, with a video-game save point analogy and a packing-a-box picture of the three areas of Git.

The idea in plain words

Think of a video game. When you reach a checkpoint the game saves your progress, and if you make a mistake later you reload the last checkpoint and carry on. Git works the same way for your files. Each save point is called a commit.

Git also has three areas, which you can picture as a desk, a box and a shelf. The working folder is your desk, where you edit files. The staging area is a box: you choose what goes into it with git add. The repository is a shelf of sealed, labelled boxes, and git commit seals the box and puts it on the shelf.

Commands in this lesson

git init
Start tracking the current folder with Git. It creates a hidden .git folder.
git status
Show which files are untracked, changed or staged. It never changes anything.
git add <file>
Stage a file: put it into the box for the next save point. Use git add . to stage everything.
git commit -m "message"
Seal the staged changes into a commit, a save point, with a short message.
git log --oneline
List every commit, newest first, one line each.

Written notes from the video

Why use Git?

Ever saved a file as final, then final version two, then final, really final? Git exists so you never have to do that again. This is Git and GitHub from zero, episode one: your first save points. By the end, you will know five commands that you will use every single day.

Git as save points

Think of a video game. You reach a checkpoint, and the game saves your progress. Later, you make a mistake and lose. No problem. You reload the last checkpoint and carry on. Git works exactly like that, but for your files. Every save point is called a commit.

Set up Git

First, install Git from the official website, git-scm.com. Then tell Git who you are. You only do this once. Type git config, with your name, and again with your email. Git stamps this on every save point, so your teammates know who made each change.

The three areas of Git

Before the commands, one idea matters most. Git has three areas. The working folder is your desk. This is where you edit your files. The staging area is a box. You choose what goes into it. The repository is a shelf of sealed, labelled boxes. Each box is one commit. So the flow is simple. Edit on your desk, pack the box, then seal it onto the shelf.

Command 1: git init

Open your project folder and run git init. Git starts watching that folder. It creates a hidden folder called .git, which is where all your save points live. You only run this once per project.

Command 2: git status

git status is your dashboard. It tells you what Git sees right now. Say you create a file called index.html. Git notices it, but marks it as untracked. That means: I can see this file, but I am not saving it yet. Use git status all the time. It is completely safe, and it never changes anything.

Command 3: git add

git add puts a file into the box. We call that staging. Run git add, then the file name. Run git status again, and the file turns green. It is now ready to be saved. To stage everything at once, use git add dot.

Command 4: git commit

git commit seals the box and puts it on the shelf. That is your save point. The -m flag lets you write a short message describing the change. Write messages like add homepage, not stuff. Future you will be grateful. Git gives the commit a short code, called a hash. That is its unique ID.

Command 5: git log

git log shows the history of every save point, newest first. Add the --oneline flag for a compact view, one commit per line. Edit the file, stage it, and commit again, and the history has two save points. Each one is a moment you can always return to.

Recap: the daily loop

git init starts tracking a project. git status shows what is going on. git add stages a change. git commit saves it. And git log shows your history. Your daily loop is simple: edit, add, commit. Then repeat.

Common questions

What is the difference between Git and GitHub?

Git is the version control tool that runs on your own computer and keeps the history of your files. GitHub is a website that stores Git repositories online so you can back them up and share them. You can use Git without GitHub. Episode 4 of this course connects the two.

Why do I need git add before git commit?

Staging lets you choose exactly which changes go into a commit. You can edit five files and commit only two of them, so each commit stays focused on one change.

What is a commit hash?

Every commit gets a long unique ID called a hash. git log --oneline shows only the first seven characters, which is enough to identify a commit in a small project.

Is git status safe to run?

Yes. git status only reads the current state of your folder and prints a report. It does not change your files or your history, so you can run it as often as you like.

What does git init create?

It creates a hidden folder named .git inside your project. That folder holds the whole history of the project, so deleting it removes Git tracking from the folder.