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

Git commit Command

git commit is like taking a snapshot of everything you picked and pasting it in a photo album with a short caption. Later you can flip back to it anytime.
Syntax
bash
git commit -m "commit message"
git commit -am "commit message"
git commit --amend

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"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running git commit without -m and being dropped into an editor like Vim, not knowing how to save and exit.
  2. Using git commit -am and expecting new untracked files to be included, when -a only stages files Git already tracks.
  3. Using git commit --amend on a commit that is already pushed, which rewrites history and forces others to deal with conflicts.

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.