Skip to the content

Lesson 93 of 296

Git: commits and history

What you will learn

  • What Git is and why every team uses it
  • The three areas: working directory, staging area, repository
  • Your first commits
  • Reading history and differences
  • .gitignore
  • Writing good commit messages
  • Fixing the last commit
On this page

What is Git?

Git is a version control system: it records every change to your project over time. With Git you can:

  • see who changed what, when and why
  • go back to any earlier version
  • work on new features without breaking the main code
  • combine work from many developers safely

Git is not GitHub. Git is the tool on your computer. GitHub (and GitLab, Bitbucket) is a website that hosts Git projects online and adds collaboration features (Lesson 95).

Key words first

  • Repository (repo): a project folder tracked by Git. Git keeps its data in a hidden .git folder inside it.
  • Commit: a saved snapshot of the project at one moment, with a message, an author, a date and a unique ID (a long code called a hash, such as a3f9c21...).

One-time setup

git config --global user.name "Sam Khan"
git config --global user.email "sam@example.com"
git config --global init.defaultBranch main

Every commit records this name and email.

The three areas

AreaWhat it is
Working directoryYour actual files, as you edit them
Staging area (also called the index)The changes you have chosen to include in the next commit
RepositoryAll the commits saved so far
edit files  -->  git add  -->  git commit
(working)        (staging)      (repository)

The staging area lets you choose exactly what goes into each commit. If you changed five files but only three are about one feature, you can stage and commit just those three.

Your first commits

mkdir shop && cd shop
git init
echo "# Shop" > README.md
git status
git add README.md
git commit -m "Add README"
CommandMeaning
git initTurn the current folder into a repo
git statusShow what has changed and what is staged. Run it often.
git add file.jsStage one file
git add src/Stage a folder
git add .Stage everything in the current folder
git add -pGo through changes piece by piece and choose what to stage
git commit -m "message"Save the staged changes as a commit

Reading history

CommandShows
git logAll commits, newest first
git log --onelineOne line per commit
git log --oneline --graph --allA drawing of all branches
git log -p file.jsThe history of one file, with changes
git show a3f9c21One commit and its changes
git blame file.jsWho last changed each line, and in which commit

Seeing differences

CommandShows
git diffChanges not yet staged
git diff --stagedChanges staged for the next commit
git diff main featureDifferences between two branches

In a diff, lines starting with - were removed and lines starting with + were added.

.gitignore

Some files must never be committed. List them in a .gitignore file at the root:

node_modules/
dist/
.env
.env.local
*.log
.DS_Store
coverage/
Never commitWhy
node_modules/Huge; recreated with npm install
Build output (dist/, .next/)Recreated by the build
.env files with secretsAnyone with access to the repo would see your passwords and API keys
Log files, OS filesNoise

If you commit a secret by mistake: removing it in a new commit is not enough, because it stays in the history. Treat the secret as leaked: change it immediately (rotate the key or password). Then clean the history if needed.

To stop tracking a file that was already committed:

git rm --cached .env

Good commit messages

A commit message explains what changed and why, for your team and for you in six months.

Rules:

  • A short first line, around 50 characters, in the imperative: "Add login form", not "Added login form" or "login stuff".
  • If needed, a blank line and then more detail explaining why.
  • One logical change per commit. Small, focused commits are easier to review and undo.
Fix VAT rounding on basket total

VAT was rounded per item, so totals could be 1p out
compared with invoices. Round once on the subtotal instead.

Many teams use Conventional Commits, a format with a type prefix:

feat: add password reset email
fix: correct VAT rounding on basket total
docs: update setup instructions
refactor: extract price formatting helper
test: add tests for discount codes
chore: update dependencies

Undoing before a commit

SituationCommand
Throw away changes in a file (not staged)git restore file.js
Unstage a file, but keep the changesgit restore --staged file.js

git restore file.js permanently discards your edits to that file. Be sure.

Fixing the last commit

git commit --amend -m "Better message"

Or stage a forgotten file, then:

git add forgotten.js
git commit --amend --no-edit

--amend replaces the last commit with a new one. Only amend commits that you have not pushed yet (Lesson 95), because it changes history that others may already have.

Common mistakes

  • Committing node_modules or .env.
  • Huge commits mixing many unrelated changes.
  • Messages like "fix", "update" or "wip".
  • git add . without checking git status first.
  • Amending a commit that is already pushed.

How to say it in an interview

"Git tracks changes in three areas: the working directory, the staging area and the repository. I stage exactly what belongs together with git add, sometimes git add -p, and make small commits with imperative messages explaining why, often in Conventional Commits format. I keep node_modules, build output and env files in .gitignore, and if a secret is ever committed, I rotate it immediately because it remains in history. I only amend commits that have not been pushed."

Key words

WordMeaning
Version controlRecording changes to files over time
RepositoryA project tracked by Git
CommitA saved snapshot with a message
HashThe unique ID of a commit
Staging areaChanges chosen for the next commit
DiffThe differences between two versions
.gitignoreA list of files Git should not track
AmendReplace the last commit

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.