Git and GitHub · Part 1 · Git basics

Making commits

What's inside a commit

Every commit holds six things:

PartWhat it is
HashA unique 40-character ID, usually shortened to 7, like 2c12ea6
SnapshotEvery tracked file, as it was at that moment
AuthorThe name and email from Lesson 2
DateWhen it was made
MessageWhy it was made
ParentThe commit just before it

The parent links commits into a chain, and that chain is the history:

  1. Add notes file

    No parent

  2. Add more notes

    Parent: Add notes file

  3. 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

Terminal
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:

Commit message
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

Terminal
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.

BadGood
fixFix login button not responding on Safari
changesAdd email validation to signup form
asdfghRemove unused lodash dependency
updated stuffUpdate hero section copy for launch
final fix pls workHandle null user in dashboard header

Five rules make the difference:

  1. Write it as a command. “Add feature”, not “Added feature”. It should finish the sentence “If applied, this commit will…”.
  2. Keep the subject short. Aim for about 50 characters; GitHub cuts off anything past 72.
  3. Capitalize it, with no period. “Add search bar”, not “add search bar.”
  4. 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”.
  5. 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.

❌ One commit doing four things
Fix login bug, add dark mode, update README, rename variables
✅ Four atomic commits
Fix 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:

Terminal
git commit --amend -m "Fix login redirect after password reset"

To add a forgotten file and keep the message (--no-edit):

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

Prefixes 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 dependencies

Practice

In git-practice:

  1. 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
  2. Edit styles.css and run git commit -a without -m. Write a subject, a blank line and a body.
  3. Make a commit with a typo in its message, then fix it with --amend.
  4. Rewrite these two messages: fixed the bug and added some stuff to the footer and also fixed header.
Show possible answers
  • fixed the bug doesn'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.