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
.gitfolder 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 mainEvery commit records this name and email.
The three areas
| Area | What it is |
|---|---|
| Working directory | Your actual files, as you edit them |
| Staging area (also called the index) | The changes you have chosen to include in the next commit |
| Repository | All 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"| Command | Meaning |
|---|---|
git init | Turn the current folder into a repo |
git status | Show what has changed and what is staged. Run it often. |
git add file.js | Stage one file |
git add src/ | Stage a folder |
git add . | Stage everything in the current folder |
git add -p | Go through changes piece by piece and choose what to stage |
git commit -m "message" | Save the staged changes as a commit |
Reading history
| Command | Shows |
|---|---|
git log | All commits, newest first |
git log --oneline | One line per commit |
git log --oneline --graph --all | A drawing of all branches |
git log -p file.js | The history of one file, with changes |
git show a3f9c21 | One commit and its changes |
git blame file.js | Who last changed each line, and in which commit |
Seeing differences
| Command | Shows |
|---|---|
git diff | Changes not yet staged |
git diff --staged | Changes staged for the next commit |
git diff main feature | Differences 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 commit | Why |
|---|---|
node_modules/ | Huge; recreated with npm install |
Build output (dist/, .next/) | Recreated by the build |
.env files with secrets | Anyone with access to the repo would see your passwords and API keys |
| Log files, OS files | Noise |
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 .envGood 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 dependenciesUndoing before a commit
| Situation | Command |
|---|---|
| Throw away changes in a file (not staged) | git restore file.js |
| Unstage a file, but keep the changes | git 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_modulesor.env. - Huge commits mixing many unrelated changes.
- Messages like "fix", "update" or "wip".
git add .without checkinggit statusfirst.- 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
| Word | Meaning |
|---|---|
| Version control | Recording changes to files over time |
| Repository | A project tracked by Git |
| Commit | A saved snapshot with a message |
| Hash | The unique ID of a commit |
| Staging area | Changes chosen for the next commit |
| Diff | The differences between two versions |
| .gitignore | A list of files Git should not track |
| Amend | Replace the last commit |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.