Lesson

Git Commit

10 min reading
Free Course

Git Commit: Recording Permanent Project Snapshots

A Git commit saves a permanent snapshot of everything currently in your staging area, creating a safe checkpoint you can inspect, compare, or return to later. Each commit records a unique SHA-1 hash, author metadata, timestamp, parent commit, and your explanatory message.

Creating Your First Commit

Once index.html and styles.css are staged, record a commit with the -m flag followed by a concise message:

git commit -m "Add initial homepage and styles.css"

Git confirms the branch, short commit hash, number of changed files, and inserted lines. If you run git commit without -m, Git opens your configured text editor so you can write a short subject line followed by a blank line and a detailed body paragraph.

Skipping Staging for Tracked Files (-a)

When you modify files that Git already tracks, you can combine staging and committing in one step using -a:

git commit -a -m "Adjust body margin in styles.css"

Keep in mind that git commit -a ignores untracked files. Any brand-new file like assets/banner.svg still requires git add before it can enter a commit.

Writing Clear Commit Messages

Good commit messages help teammates and your future self understand why a change happened without reading every line of diff:

  • Use the imperative mood in the subject line: write "Fix mobile navbar overlap" instead of "Fixed overlap" or "Updating navbar".
  • Keep the subject line under 50–72 characters so it does not truncate in git log --oneline or GitHub pull requests.
  • Focus each commit on one logical change so reverting a bug does not undo unrelated features.

Common Commit Mistakes

Committing with vague messages like "wip", "update", or "fix" makes debugging months later slow and frustrating. Take ten seconds to name the module and the problem solved.

Avoid bundling formatting changes, dependency upgrades, and feature code into a single giant commit. Split them into smaller staged commits so code reviewers can verify each step quickly.

Frequently Asked Questions

What happens if I run git commit with nothing staged?

Git refuses to create an empty commit by default and prints "nothing to commit, working tree clean" or lists unstaged changes. You must run git add first unless you explicitly pass --allow-empty.

How do I write a multi-line commit message from the terminal?

You can pass multiple -m flags, such as git commit -m "Add header layout" -m "Centers navigation links on desktop screens", or omit -m to compose the message in your editor.

Can I edit a commit message right after creating it?

Yes, if you have not pushed the commit yet. Run git commit --amend -m "New message" to replace the most recent commit message cleanly.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum