Git Course 0%

git commit

Beginner Git CLI Safe ≈ 7 min

Summary: git commit turns the staging area into a new commit on the current branch, with your identity, the date and a message. Local only: nothing leaves your computer until you push.

Safe for ordinary commits — they only add history. --amend rewrites the last commit and is safe only before pushing.

What it does

Writes the staged snapshot into .git/objects as a commit whose parent is the current HEAD, then moves the current branch (and HEAD with it) to the new commit. Unstaged changes stay in the working tree.

Why it exists

A commit is the unit of history: what gets reviewed, reverted, tagged and read. Recording snapshots on demand, with messages, is the whole point of version control.

When to use it

  • Whenever a coherent piece of work is done: a fix, a section, a data update. Small and often.
  • Before switching branches, pulling or leaving for the day (a wip: commit on your own branch is fine).

When not to use it

  • With nothing staged: it does nothing (see below).
  • --amend on a commit that has been pushed to a shared branch (lesson 2.7).

Syntax

Form Meaning
git commit -m "<subject>" Commit staged changes with a message
git commit -m "<subject>" -m "<body>" Subject plus a body paragraph
git commit Open the editor for the message
git commit -a / -am "…" Also stage every modified or deleted tracked file first (never new files)
git commit --amend Replace the last commit with staged changes and/or a new message
git commit --amend --no-edit Amend keeping the message
git commit --allow-empty -m "…" A commit with no changes (practice, or a marker)

Examples

Terminal
$ git commit -m "docs: describe the region and answer a data question (#28)"
[main dcc61f1] docs: describe the region and answer a data question (#28)
 2 files changed, 5 insertions(+), 1 deletion(-)

With a body:

Terminal
$ git commit -m "feat: add Eagle Ridge trail (#21)" -m "Requested by the North region office. Distance measured on 2026-09-01."
[main 79c86c5] feat: add Eagle Ridge trail (#21)
 1 file changed, 1 insertion(+)

All tracked changes at once:

Terminal
$ git commit -am "docs: add maintainer note"
[main c541c80] docs: add maintainer note
 1 file changed, 2 insertions(+)

Nothing staged:

Terminal
$ git commit -m "test"
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

Amend the last commit to include a forgotten file:

Terminal
$ git add docs/faq.md
$ git commit --amend --no-edit
[main 8330ba9] feat: add Coast Walk trail (#29)
 Date: Tue Sep 8 10:00:00 2026 +0200
 2 files changed, 5 insertions(+)

Expected result

A new line at the top of git log --oneline; git status clean (if everything was staged) and, with a remote, "ahead of 'origin/main' by 1 commit". The output line shows the branch, the new short hash and the summary.

Try it here

The simulator below runs a small model of Git in the page. Nothing here touches a real repository, so try the command, then try it wrong (lesson 2.8 explains it).

The Git state simulator loads here.

Common mistakes

  • no changes added to commit. Nothing was staged: git add first.
  • An editor opened. -m was omitted. Write the message and close the editor; close without saving to abort.
  • Please tell me who you are. Identity not configured (lesson 3.3).
  • Amending after pushing. Diverged branch; use a new commit or git revert.
  • Vague messages. fix, update, wip tell nobody anything next month; see the conventions in lesson 5.4.

How to undo or recover

  • Wrong message or forgotten file, not pushed: git commit --amend.
  • Wrong commit, pushed or older: git revert <hash> adds a commit that undoes it.
  • Commit made on the wrong branch: lesson 12.7.
  • Uncommit but keep the changes staged (not pushed): git reset --soft HEAD~1 (lesson 12.4).

In VS Code

Type the message in the Source Control box and click Commit (Ctrl+Enter / ⌘Enter); … → Commit (Amend) for amend. See Edit, diff, stage, commit, push, pull, sync.

In IntelliJ IDEA

Commit tool window → message → Commit (Ctrl+K / ⌘K); tick Amend to amend. See Edit, diff, stage, commit, push, update, fetch.

git add stages what commit records · git status and git diff --staged show it first · git log shows the result · git revert undoes a commit safely · git push shares it.

Lessons that use this command

Committing and good messages · Commits, hashes and history · Undo, beginner edition · Lab 1.

Key terms

Commit Hash (SHA) HEAD Staging area