← Back to Git Course | Chapter 2: Basic Commands | Lesson 8 of 18

Git commit Command

Committing with a Message

git commit -m "message" saves everything currently staged as a new permanent snapshot in your project's history. The message isn't optional busywork -- a specific, honest description of what changed is what makes git log useful months later.

Example: Committing with a Message

bash
git commit -m "Fix login button alignment"

Staging and Committing in One Step

git commit -am "message" combines staging and committing for files Git already tracks, skipping the separate git add step -- but note the shortcut only picks up modifications to tracked files, it will never stage a brand-new untracked file for you.

Example: Staging and Committing in One Step

bash
git commit -am "Update styles"

Amending the Last Commit

git commit --amend replaces your most recent commit entirely -- with a corrected message, an added forgotten file, or both -- rather than creating a new commit. It's convenient for fixing a mistake seconds after making it, but avoid amending a commit you've already pushed and shared.

Example: Amending the Last Commit

bash
git commit --amend

Committing with a Detailed Description

For a change that needs more explanation than one line, running git commit -m "Title" -m "Longer paragraph explaining why" creates a commit with a short summary line and a separate detailed body, matching the conventional format most Git tools expect.

Example: Committing with a Detailed Description

bash
git commit -m "Fix login bug" -m "The button was not responding on mobile Safari due to a missing touch event handler."

Empty Commits

git commit --allow-empty creates a commit with zero file changes, which sounds useless but is genuinely handy for triggering a CI/CD pipeline manually, or for marking a meaningful milestone in history without altering any code.

Example: Empty Commits

bash
git commit --allow-empty -m "Trigger CI pipeline"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.