Testing and Git · Part 17 of 36

Git: version control from the command line

Intermediate 16 min

Repositories, commits, the staging area, history, undoing mistakes and .gitignore — the Git commands you need every day.

What you will learn

  • You can create a repository and make commits.
  • You can explain working tree, staging area and history.
  • You can inspect history and undo changes safely.

Before this: Automated tests and test-driven development

What version control gives you

Git records snapshots of your project over time. With it you can see who changed what and why, return to any earlier state, work on several ideas in parallel and merge other people's work. Linus Torvalds wrote Git in 2005 for the Linux kernel; today it is the standard almost everywhere.

Beads on a string with side branches that split off and merge back into the main line

Three places

Git thinks in three areas:

  1. Working tree — the files as you edit them.
  2. Staging area (index) — the changes you have selected for the next commit.
  3. Repository history — the commits, stored in the hidden .git folder.
Working tree, staging area and history with arrows for git add and git commit
git add moves changes to the staging area, git commit records them

Everyday commands

git init                      # start a repository in this folder
git status                    # what changed? what is staged?
git add garden.py             # stage a file (git add -p to pick parts)
git commit -m "Add area calculation for beds"
git log --oneline             # history, newest first
git diff                      # unstaged changes
git diff --staged             # staged changes

A commit is a snapshot plus a message, an author, a date and a pointer to its parent commit. Its id is a hash of all that (SHA-1 by default; Git also supports SHA-256 repositories, and Git 3.0 is planned to make SHA-256 the default). Change anything in history and the ids change — that's what makes Git history tamper-evident.

Good commits

  • Small and focused: one logical change per commit, so it is easy to review and to undo.
  • Clear messages: a short summary line in the imperative ("Fix rounding in totals"), then, if needed, a blank line and the why.
  • Never commit secrets (passwords, API keys). Once pushed, assume they are leaked — rotate them.

Ignoring files

A .gitignore file lists what Git should never track: build output, dependencies (node_modules/, .venv/), editor files and local configuration with secrets (.env).

Undoing things

SituationCommand
Throw away unstaged edits to a filegit restore garden.py
Unstage a file (keep the edits)git restore --staged garden.py
Fix the last commit's message or add a forgotten filegit commit --amend (only before you share it)
Undo a commit that is already sharedgit revert <commit> — makes a new commit that reverses it
Look at an old versiongit switch --detach <commit> or git show <commit>:garden.py

The golden rule: never rewrite history that others already have (amend, rebase, reset --hard on pushed commits). revert is the safe way to undo shared work.

Remotes

A remote is another copy of the repository, usually on a hosting service. git clone <url> copies one; git push sends your commits; git pull fetches and integrates others' commits. Git itself is fully local and works offline — hosting services add collaboration features on top, covered in the next lesson.

Key points

  • Git stores snapshots (commits) of your project with author, date, message and parent.
  • Working tree → git add → staging area → git commit → history.
  • Make small, focused commits with clear messages; never commit secrets.
  • .gitignore keeps build output, dependencies and secrets out of the repository.
  • Undo locally with restore/amend; undo shared commits with revert.

Flashcards

Question: What does git add do?
Answer: Moves changes into the staging area for the next commit.
Question: How do you undo a commit that others already pulled?
Answer: git revert <commit> — it creates a new commit that reverses it.
Question: What belongs in .gitignore?
Answer: Build output, dependencies, editor files and local secrets such as .env.
Question: What is a commit id?
Answer: A hash of the snapshot, metadata and parent — it changes if anything in history changes.

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. Order the path of a change into Git history.

    Show the answer

    Edit the file in the working tree → git add → git commit

    Changes go from the working tree to the staging area to history.

    Source: Pro Git, 2.2

  2. You pushed a commit with a bug and colleagues have pulled it. How do you undo it safely?

    • git revert
    • git reset --hard
    • git commit --amend
    • Delete the .git folder
    Show the answer

    git revert

    revert adds a new commit and leaves shared history intact.

    Source: Pro Git, 7.8 / git-revert docs

  3. Which of these should usually be in .gitignore?

    • node_modules/
    • .env with API keys
    • README.md
    • build output folder
    Show the answer

    node_modules/; .env with API keys; build output folder

    Dependencies, secrets and generated files stay out; documentation belongs in the repository.

    Source: Pro Git, 2.2 Ignoring Files

  4. To see unstaged changes line by line you run git ___.

    Show the answer

    diff

    git diff shows changes in the working tree not yet staged.

    Source: Git reference, git-diff

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.