Skip to main content

Getting Started with Git · Chapter 3 / 5

Save your changes

A commit is a saved version of your project. You'll learn to create commits, read the history, fix mistakes and send everything to GitHub.

Chapter 3 / 5

Step 01 / 13

In this chapter

Your files are ready in the staging area. It's time to record them for good in your project's history, then send them to GitHub.

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

  • Create a commit and write a clear message
  • Read the history with git log
  • Fix your last commit
  • Go back to a previous version
  • Ignore files with .gitignore
  • Send your commits to GitHub with git push, and get other people's commits with git pull

Step 02 / 13

What is a commit?

Concept

Commit: a saved version of your project. It's a complete photo of all your tracked files at the moment you take it.

A commit contains:

  • A photo (called a snapshot) of all your tracked files
  • A message explaining what you did
  • The author (the name and email you set up in chapter 1) and the date
  • A unique identifier, called a hash, for example 8c4e2f1
  • A link to the previous commit, its parent: this chain of links is what forms the history

Here is what a commit looks like in the history:

# A commit, as Git displays it

commit 8c4e2f1a7b3d9e0f5a6c2b1d4e7f8a9b0c1d2e3f (HEAD -> main)
Author: Camille Martin <camille@example.com>
Date:   Mon Oct 19 10:24:18 2026 +0200

    Add contact form

The analogy

In the photographer's notebook, a commit is a page: the photo glued in, the date and the caption written underneath. The pages follow one another in order, and you never tear one out.

Step 03 / 13

Create a commit: git commit

A commit saves everything in the staging area. So you need to run git add first. The command:

$ git commit -m "<message>"

The -m option (for message) lets you write the message directly in the command. For example, for the very first commit of my-portfolio:

$ git add index.html style.css README.md

$ git commit -m "Add home page"

[main (root-commit) 3f2a1b4] Add home page
 3 files changed, 42 insertions(+)
 create mode 100644 README.md
 create mode 100644 index.html
 create mode 100644 style.css

Git replies with a summary:

  • main: the branch the commit was created on
  • root-commit: this is the very first commit in the repository (you'll only see this label once)
  • 3f2a1b4: the first characters of the commit's identifier
  • 3 files changed, 42 insertions(+): 3 files affected, 42 lines added

Tip

If you forget -m, Git opens a text editor in the terminal so you can write the message. If you're stuck in that editor (often Vim): press Esc, type :q! then Enter. This exits without committing, and you can run the command again with -m.

If the staging area is empty, Git doesn't create anything and tells you so:

$ git commit -m "Update the menu"

On branch main
Changes not staged for commit:
	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

What does git commit save?

Step 04 / 13

Write a good commit message

Six months from now, you or a teammate will read the history to understand what happened. That's what the message is for. A few simple rules:

  • Start with a verb in the imperative: Add, Fix, Remove, Update.
  • Keep it short: about 50 characters at most.
  • Be specific: people should understand the change without opening the files.
  • One commit = one topic. If your message contains "and", it might really be two commits.

A few examples:

  • Good: "Fix the menu display on mobile"
  • Good: "Add contact form"
  • Avoid: "fix" (nobody knows what was fixed)
  • Avoid: "changes" or "wip" (tells you nothing)
  • Avoid: "Add the form and fix the menu and change the colors" (three topics)

Practice

Commit often, in small steps. A small commit is easy to understand, review and undo if needed.

Which commit message is the most useful?

Step 05 / 13

See the history: git log

git log lists the commits, from newest to oldest:

$ git log

commit 8c4e2f1a7b3d9e0f5a6c2b1d4e7f8a9b0c1d2e3f (HEAD -> main)
Author: Camille Martin <camille@example.com>
Date:   Mon Oct 19 10:24:18 2026 +0200

    Add contact form

commit 5b9d3a7e2c1f8b4a6d0e9c3f7a2b5d8e1c4f6a90
Author: Camille Martin <camille@example.com>
Date:   Sun Oct 18 16:02:51 2026 +0200

    Fix the mobile menu

For a compact view, one line per commit:

$ git log --oneline

8c4e2f1 (HEAD -> main) Add contact form
5b9d3a7 Fix the mobile menu
3f2a1b4 Add home page

(HEAD -> main) shows where you are: on the main branch, at the most recent commit. We'll come back to HEAD in chapter 4.

Tip

When the history is long, git log displays it page by page. Scroll with the arrow keys or the space bar, and press q to quit.

To see the details of a specific commit, use git show with its identifier:

$ git show 5b9d3a7

# Shows the commit message, then the changed lines (like git diff)

git log shows a long list and you can't type commands anymore. What do you do?

Step 06 / 13

Fix the last commit: --amend

A typo in the message? A forgotten file? As long as you haven't pushed the commit to GitHub, you can fix the last commit with the --amend option.

Change the message of the last commit:

$ git commit --amend -m "Add contact form"

Add a forgotten file to the last commit, without changing the message:

$ git add contact.css

$ git commit --amend --no-edit

Warning

--amend replaces the last commit with a new one, with a new identifier. Don't use it on a commit you've already pushed with git push: GitHub's history would no longer match yours.

“I can safely use --amend on a commit I've already pushed with git push.” True or false?

Step 07 / 13

Go back to a previous version

Want to undo one or more commits? There are two cases.

Case 1: the commits are only on your computer (not pushed yet). Use git reset with the identifier of the commit you want to go back to:

$ git reset <commit-id>

$ git reset 5b9d3a7

Unstaged changes after reset:
M	index.html

The commits that came after 5b9d3a7 disappear from the history. But their changes aren't lost: they're back in your files, as if you hadn't committed them yet. You can rework them, then make a clean commit.

Warning

You'll sometimes see git reset --hard. This option also wipes out the changes in your files, and you can't get them back. Avoid it while you're learning.

Case 2: the commits are already on GitHub. Don't rewrite history that others share. Use git revert instead, which creates a new commit that does the exact opposite:

$ git revert --no-edit 8c4e2f1

[main 1e6b2d9] Revert "Add contact form"
 1 file changed, 30 deletions(-)

The --no-edit option keeps Git's default message. The mistake stays visible in the history, with its fix right after it, so everyone can see what happened.

Tip

To find a commit's identifier, use git log --oneline. The first 7 characters are enough.

The commit to undo is already on GitHub. Which command should you use?

Step 08 / 13

Ignore files: .gitignore

Some files should never be committed to Git:

  • Secret files: passwords, access keys (often in a .env file)
  • Downloaded dependencies, which are heavy and easy to reinstall: the node_modules/ folder
  • Files created automatically by your system or tools: .DS_Store on macOS, .log files

To make Git ignore them, create a file named .gitignore at the root of the project, and write one pattern per line:

# Content of the .gitignore file

# Secrets
.env

# Dependencies
node_modules/

# System files and logs
.DS_Store
*.log

# Exception: keep this one
!important.log

How to read these patterns:

  • .env: ignores the .env file
  • node_modules/: ignores the whole node_modules folder
  • *.log: the asterisk matches any name, so every file ending in .log
  • !important.log: the exclamation mark creates an exception: this file is kept
  • #: a comment line, ignored by Git

The .gitignore file itself should be committed, so the whole team benefits from it.

$ git add .gitignore

$ git commit -m "Add .gitignore file"

Warning

.gitignore only affects files that aren't tracked yet. If you already committed a file by mistake, stop tracking it with git rm --cached <file> (it stays on your disk), then commit. And if it was a password, change it: it's still visible in the history.

.env
— Ignores the .env file
node_modules/
— Ignores the whole node_modules folder
*.log
— Ignores every file ending in .log
!important.log
— Keeps this file despite the *.log rule

Step 09 / 13

Send your commits to GitHub: git push

Your commits are on your computer. To send them to the remote repository you linked in chapter 1, use git push. The first time, tell Git where to send them:

$ git push -u origin main

Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Writing objects: 100% (9/9), 1.20 KiB | 1.20 MiB/s, done.
Total 9 (delta 1), reused 0 (delta 0)
To https://github.com/camille-dev/my-portfolio.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

origin is the name of the remote repository, and main is the branch. The -u option saves this link, so from then on, a plain git push is enough.

$ git push

Tip

The first time, GitHub asks you to sign in. Your GitHub password doesn't work in the terminal. The easiest option: install GitHub CLI and run gh auth login, or use GitHub Desktop. You can also create a personal access token in your GitHub settings.

Going the other way, to get the commits others have pushed to GitHub:

$ git pull

Updating 8c4e2f1..d71a3c9
Fast-forward
 README.md | 4 ++++
 1 file changed, 4 insertions(+)

Practice

Good habit: git pull before you start working, git push when you're done. Your local repository and GitHub stay in sync.

What does the -u option do in git push -u origin main?

Step 10 / 13

Recap: saving your changes

1

git add: you stage your files

2

git commit -m "message": you save a version

3

git log --oneline: you check the history

4

git push: you send your commits to GitHub

Your cheat sheet for this chapter:

$ git commit -m "message" # commit what is staged

$ git commit --amend -m "message" # fix the last commit

$ git log # detailed history

$ git log --oneline # compact history

$ git show <id> # show details of a commit

$ git reset <id> # go back to a commit (not pushed)

$ git revert <id> # undo a commit already pushed

$ git rm --cached <file> # stop tracking a file

$ git push # send commits to the remote

$ git pull # get commits from the remote

git commit -m
— Save a version
git log --oneline
— See a compact history
git commit --amend
— Fix the last commit
git revert
— Undo a commit already shared
git push
— Send your commits to GitHub

Step 11 / 13

Common mistakes

  • Committing without git add: Git answers "no changes added to commit". Stage your files with git add first.
  • Vague messages like "fix" or "update": a month later, nobody will know what they contain.
  • Making one huge commit at the end of the day: make several small commits instead, one per topic.
  • Using --amend or git reset on commits you've already pushed: your history no longer matches GitHub's. Use git revert instead.
  • Committing a .env file with passwords: add it to .gitignore as soon as you start the project.
  • Getting "rejected (fetch first)" when you run git push: GitHub has commits you don't have yet. Run git pull, then git push.

Step 12 / 13

Bonus: under the hood

Tip

Optional section for the curious. You can skip straight to the quiz.

Every commit has an identifier like 8c4e2f1a7b3d9e0f5a6c2b1d4e7f8a9b0c1d2e3f. It's called a hash (or SHA). Git computes it from the commit's content: the files, the message, the author, the date and the parent.

As a result, if a single character changes, the hash changes completely. It works like a fingerprint: two different contents don't get the same hash, so Git spots any change immediately.

A commit is built from several objects stored in .git/objects:

  • Blob: a file's content (without its name)
  • Tree: a folder, that is, the list of file names and their blobs
  • Commit: a link to the top-level tree, plus the message, the author, the date and the parent

commit

message, author, date, parent

points to

tree

the folder (names + links)

contains

blob

the raw file content

A commit points to a tree, which lists blobs (and other trees for subfolders).

You can inspect the last commit with git cat-file:

$ git cat-file -p HEAD

tree 9f3b2e1d7c6a5b4e8f0d1c2a3b4e5f6a7b8c9d0e
parent 5b9d3a7e2c1f8b4a6d0e9c3f7a2b5d8e1c4f6a90
author Camille Martin <camille@example.com> 1792398258 +0200
committer Camille Martin <camille@example.com> 1792398258 +0200

Add contact form

Concept

If a file didn't change between two commits, Git doesn't store it again: it reuses the same blob. Every commit is a complete photo of the project, yet storage stays light.

$ git hash-object

0d462e0

The hash depends on the entire content: add, remove, or capitalize a single letter, and the result is completely different. But the same content always gives the same hash. That's what guarantees the integrity of your commit history.

Step 13 / 13

Test your knowledge

Check that you can save, fix and share your versions. Click the answer you think is right: each one comes with an explanation.

Quiz · Chapter 3

1. What does git commit -m "message" do?

2. Which commit message is the most useful?

3. You just made a commit (you haven't pushed it) and forgot a file. What do you do?

4. A bad commit has already been pushed to GitHub. What's the safe way to undo it?

5. Which .gitignore line ignores every file ending in .log?

6. What is the -u option for in git push -u origin main?

Chapter 3 of 5

The investigation is just getting started. The next clue is waiting...

Your feedback matters

Help us improve this course by sharing your thoughts.

So we can follow up if needed.