Git & GitHub ยท Chapter 7 of 42

git commit

A commit is a saved snapshot of your staged changes, along with a message describing what changed. Each commit has a unique ID (a hash) and forms part of the project's permanent history.

Commits should be small and focused, capturing one logical change at a time. This makes it much easier to review history, find bugs, or undo specific changes later.

Syntax
git commit -m "message"

Making a commit

After staging changes with git add, run `git commit -m "message"` to save a snapshot with a short description of the change.

Commit hashes

Every commit gets a unique SHA-1 hash, like a3f5c9e. You can use a shortened version of this hash to refer to a specific commit in other commands.

Example 1 (bash)
git add app.js
git commit -m "Add login validation"
Output
[main a3f5c9e] Add login validation
 1 file changed, 10 insertions(+)

Commits the staged change with a descriptive message and shows the new commit hash.

Example 2 (bash)
git commit -am "Fix typo in README"
Output
[main b7e21ff] Fix typo in README
 1 file changed, 1 insertion(+), 1 deletion(-)

The -a flag automatically stages tracked, modified files, combining add and commit in one step.

Key points

  • git commit saves a snapshot of staged changes.
  • Each commit needs a message describing the change.
  • Every commit has a unique hash identifier.
  • -am stages tracked changes and commits in a single command.
๐Ÿ’ก Note: git commit -a only stages files Git already tracks โ€” new untracked files still need git add first.

๐Ÿ“ Quick Quiz

1. What does git commit do?

2. What flag lets you skip a separate git add for tracked files?

3. What uniquely identifies a commit?