Git and GitHub · Part 1 · Git basics
Making commits
What's inside a commit
Every commit holds six things:
| Part | What it is |
|---|---|
| Hash | A unique 40-character ID, usually shortened to 7, like 2c12ea6 |
| Snapshot | Every tracked file, as it was at that moment |
| Author | The name and email from Lesson 2 |
| Date | When it was made |
| Message | Why it was made |
| Parent | The commit just before it |
The parent links commits into a chain, and that chain is the history:
Add notes file
No parent
Add more notes
Parent: Add notes file
Fix typo
Parent: Add more notes
The first commit has no parent. Every later one points back to the one before it.
The hash is calculated from everything in the commit. Change one letter and the hash changes completely, so history can't be altered quietly.
Three ways to commit
1.A short message with -m
git commit -m "Fix navbar overflow on mobile"For small, obvious changes.
2.A longer message in the editor
git commit without -m opens the editor. Write the message, then save and close the file:
Fix navbar overflow on mobile
The menu items were wrapping onto two lines on screens
under 375px because of fixed padding. Switched to gap
and made the logo shrink on small screens.The first line is the subject. After a blank line comes the body, which explains why. Use this for any change that needs explaining.
3.The shortcut: -am
git commit -am "Update notes"-a stages every changed file Git already tracks, so git add can be skipped. New files still need git add first.
Writing good commit messages
Months later, someone will read git log to find out why something changed. A clear message saves them hours.
| Bad | Good |
|---|---|
fix | Fix login button not responding on Safari |
changes | Add email validation to signup form |
asdfgh | Remove unused lodash dependency |
updated stuff | Update hero section copy for launch |
final fix pls work | Handle null user in dashboard header |
Five rules make the difference:
- Write it as a command. “Add feature”, not “Added feature”. It should finish the sentence “If applied, this commit will…”.
- Keep the subject short. Aim for about 50 characters; GitHub cuts off anything past 72.
- Capitalize it, with no period. “Add search bar”, not “add search bar.”
- Explain why in the body. The code shows what changed. “Increase API timeout because reports take up to 8s” beats “Changed timeout from 5000 to 10000”.
- Wrap the body at about 72 characters, so it reads well in the terminal.
Atomic commits: one commit, one purpose
An atomic commit does one thing, like one step in a recipe. This matters even more than the message.
Fix login bug, add dark mode, update README, rename variablesFix login redirect after password reset
Add dark mode toggle to settings
Update README with setup steps
Rename userData to currentUser for clarity- Easy to undo: if dark mode breaks something, only that commit is undone, and the login fix stays.
- Easy to review: small changes are quick to understand.
- Easy to debug: tools like
git bisect, which find the commit that caused a bug, work best with small commits.
The staging area is what makes this possible: stage one group of related changes, commit, then the next. Commit whenever a small step is done and working.
Fixing the last commit with --amend
To fix a typo in the last commit's message:
git commit --amend -m "Fix login redirect after password reset"To add a forgotten file and keep the message (--no-edit):
git add forgotten-file.js
git commit --amend --no-editPrefixes like feat: and fix:
Many teams start messages with a prefix for the kind of change, a format called conventional commits. The text after the prefix is usually lowercase:
feat: add dark mode toggle
fix: handle null user in header
docs: update README setup steps
chore: upgrade dependenciesPractice
In git-practice:
- Create three files, then commit each one separately with a good message:
Terminal echo "home page" > home.html echo "about page" > about.html echo "body { margin: 0; }" > styles.css - Edit
styles.cssand rungit commit -awithout-m. Write a subject, a blank line and a body. - Make a commit with a typo in its message, then fix it with
--amend. - Rewrite these two messages:
fixed the bugandadded some stuff to the footer and also fixed header.
Show possible answers
fixed the bugdoesn't say which bug. Name it: Fix crash when cart is empty.- The second is two changes, so it should be two commits: Add newsletter signup to footer and Fix header overlapping content on scroll.