Git Commit
A writer working on a long report doesn't wait until the last page to save. They save at every point where the work makes sense: a finished section, a corrected table, a new introduction. Each save is a place they can return to.
In Git, that save is a commit. git commit takes everything in the staging area and stores it permanently in the repository as one snapshot, with your name, the time and a message that says what you did.
In this lesson, you will learn the forms of git commit you will use every day, what Git does when you leave out the message, and how to repair the commit you made a moment ago.
Make a Commit
A commit always follows the same two steps: stage with git add, then save with git commit.
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit -m "Add project README"[main (root-commit) dd34d8a] Add project README
1 file changed, 1 insertion(+)
create mode 100644 README.mdGit's answer is a short receipt:
| Part | Meaning |
|---|---|
main | The branch the commit was added to |
(root-commit) | This is the first commit of the repository. Later commits don't show this. |
dd34d8a | The short ID of the new commit |
Add project README | The message |
1 file changed, 1 insertion(+) | How many files changed, and how many lines were added or removed |
create mode 100644 README.md | A file that is new in this commit |
Syntax
git commit -m "<message>"| Option | It is used to |
|---|---|
-m "<message>" | Give the commit message on the command line |
no -m | Open your editor so you can write a longer message |
-a | Stage every modified and deleted tracked file first, then commit |
-v | Show the changes inside the editor while you write the message |
--amend | Replace the last commit with a corrected one |
A Commit Saves What Is Staged
git commit looks only at the staging area. If nothing is staged, there is nothing to commit, even when you have edited files:
echo "Tracks school fee payments." >> README.md
git commit -m "Describe what the app does"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")No commit was made. Git printed the status and reminded you, in the last line, what to do: git add first.
Commit Without -m: The Editor Opens
After staging the file, Navika runs git commit with no message. Git opens her editor with a file to fill in. To show you that file on this page, the command below tells Git to use cat as the editor for one command. cat prints the file and closes it at once:
git add README.md
GIT_EDITOR=cat git commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Changes to be committed:
# modified: README.md
#
Aborting commit due to empty commit message.How it works
- The first line is empty. That is where you type your message.
- The lines that begin with
#are notes from Git: instructions, and a copy ofgit statusto remind you what is in the commit. They are not saved in the message. - You write the message, save the file and close the editor. Git then makes the commit.
- Here nobody typed anything, so the message was empty and Git answered
Aborting commit due to empty commit message.No commit was made.
That last point is useful. Closing the editor with an empty message is the way to cancel a commit.
A Message with a Subject and a Body
Short messages fit on the command line. For more, you can give -m twice. The first becomes the subject line and the second becomes the body, a paragraph of explanation:
git commit -m "Describe what the app does" -m "New team members asked what the project is for.
The README now says it in one line."
git log -1[main 5cfd52d] Describe what the app does
1 file changed, 1 insertion(+)
commit 5cfd52df51728e1a02b40b711f4f364948c87a65 (HEAD -> main)
Author: Navika Anamaneni <navika@example.com>
Date: Mon Jan 5 10:14:00 2026 -0600
Describe what the app does
New team members asked what the project is for.
The README now says it in one line.git log -1 shows the newest commit in full: its ID, the author, the date, then the subject, an empty line and the body. The next lesson, "Writing Good Commit Messages", is about what to write in each part.
Stage and Commit Together with -a
When every change you made is in files Git already tracks, -a saves you a step. It stages all modified and deleted tracked files and commits them:
echo "Built by Navika, Srihitha and Advitha." >> README.md
echo 'Term 1 fee: $12,000' > fees.txt
git commit -a -m "Add the team names"
git status -s[main 30f9580] Add the team names
1 file changed, 1 insertion(+)
?? fees.txtThe commit contains the change to README.md. It does not contain fees.txt, which is still untracked afterward. -a never adds new files, for the same reason as git add -u: Git will not start tracking a file unless you ask.
You will often see the two options written together as git commit -am "message". It means the same.
Fix the Last Commit with --amend
Everyone commits too early now and then: a spelling mistake in the message, or a file that was left out. If it is your most recent commit and you have not shared it yet, git commit --amend lets you redo it.
Navika commits the fee list with a typing mistake in the message:
git add fees.txt
git commit -m "Add fee lisst"
git log --oneline[main 5656142] Add fee lisst
1 file changed, 1 insertion(+)
create mode 100644 fees.txt
5656142 (HEAD -> main) Add fee lisst
30f9580 Add the team names
5cfd52d Describe what the app does
dd34d8a Add project READMEFix the Message
git commit --amend -m "Add fee list"
git log --oneline[main ba950dc] Add fee list
Date: Mon Jan 5 10:23:00 2026 -0600
1 file changed, 1 insertion(+)
create mode 100644 fees.txt
ba950dc (HEAD -> main) Add fee list
30f9580 Add the team names
5cfd52d Describe what the app does
dd34d8a Add project READMEThe message is corrected, and there are still four commits, not five. Look at the top ID: it was 5656142 and it is now ba950dc. Git did not edit the old commit. It made a new commit with the corrected message and moved main to it.
Add a Forgotten File
The same command adds a file you forgot. Stage it, then amend. --no-edit keeps the message as it is:
echo 'Term 2 fee: $12,500' > fees-term2.txt
git add fees-term2.txt
git commit --amend --no-edit
git log --oneline[main 676df16] Add fee list
Date: Mon Jan 5 10:23:00 2026 -0600
2 files changed, 2 insertions(+)
create mode 100644 fees-term2.txt
create mode 100644 fees.txt
676df16 (HEAD -> main) Add fee list
30f9580 Add the team names
5cfd52d Describe what the app does
dd34d8a Add project READMEThe last commit now has two files, the ID changed once more, and the history is still four commits long.
If you amend by mistake, the replaced commit is not destroyed right away. Git keeps it for some time, and the lessons on the reflog show how to get it back.
Common Mistakes
Committing without checking what is staged. git commit saves the staging area, whatever is in it. Run git status first.
Expecting -a to include new files. It doesn't. If git status -s still shows ?? after your commit, that file was not part of it.
One huge commit at the end of the day. A commit named "today's work" with thirty files can't be reviewed and can't be undone in part. Commit each finished piece of work when it is finished.
Amending a pushed commit. The new commit has a different ID, so your history and your teammates' histories split. Make a new commit that fixes the problem.
Panicking when the editor opens. Write the message, save, close. To cancel, close it with the message empty.
Thinking a commit is uploaded. A commit is saved in the repository on your computer. Others see it only after you push.
Under the Hood
A commit is a small text object. Here is the one Navika amended:
git cat-file -p HEADtree 2be393c55247742bbbfb2b01d36b2c820d6ce677
parent 30f9580345244645fc788abeb07527027297bb70
author Navika Anamaneni <navika@example.com> 1767630180 -0600
committer Navika Anamaneni <navika@example.com> 1767630660 -0600
Add fee list| Line | What it holds |
|---|---|
tree | The ID of the snapshot: the content of the staging area when the commit was made |
parent | The ID of the commit before this one. The first commit of a repository has no parent. |
author | Who wrote the change, and when |
committer | Who created this commit object, and when |
| After the empty line | The message |
Two things are worth noticing. The commit doesn't store a list of changed lines. It points to a full snapshot, and Git works out the changes by comparing it with the parent's snapshot. And the author time and the committer time are different here: the change was first written at one moment, and the commit object was recreated later by --amend. That is why the amended commits above printed a Date: line with the original time.
Interview Corner
What does git commit do?
It creates a new commit object from the content of the staging area: a snapshot (tree), a pointer to the parent commit, the author and committer with timestamps, and the message. Then it moves the current branch to the new commit. It works entirely in the local repository.
What is the difference between git commit -m and git commit -am?
-m commits only what has been staged with git add. Adding -a first stages every modified and deleted tracked file, then commits. It does not include untracked (new) files.
What does git commit --amend do, and when is it unsafe?
It replaces the most recent commit with a new one that combines the old commit's content, anything currently staged, and optionally a new message. Because the result is a new commit with a new ID, it rewrites history. It is safe for commits that have not been pushed, and unsafe for commits that others may already have based work on.
How do you cancel a commit while the editor is open?
Close the editor with an empty message (delete any text you typed and save). Git prints "Aborting commit due to empty commit message" and nothing is committed.
What is the difference between the author and the committer of a commit?
The author is the person who originally wrote the change, and the committer is the person who created the commit object. They are usually the same. They differ when a commit is amended, rebased or cherry-picked, or when someone applies another person's patch.
Practice
Challenge: what does -a take? Continue in the same repository. Create a new file FeeCalculator.java, add the line Late fee: $250 to fees.txt, and commit with git commit -a -m "Add late fee". Then print the short status.
What does Git print? Before you open the solution, decide how many files the commit reports as changed, and what git status -s shows afterward.
Show solution
echo "class FeeCalculator {}" > FeeCalculator.java
echo 'Late fee: $250' >> fees.txt
git commit -a -m "Add late fee"
git status -s[main f7b3692] Add late fee
1 file changed, 1 insertion(+)
?? FeeCalculator.javaOne file changed. -a staged and committed fees.txt, which Git was already tracking. FeeCalculator.java is new, so it was left out and is still untracked. To commit it, run git add FeeCalculator.java and make another commit.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git commit -m "message" | Commit what is staged, with a message |
git commit | Commit and write the message in your editor |
git commit -m "subject" -m "body" | Commit with a subject line and a body |
git commit -a -m "message" | Stage all changes to tracked files and commit |
git commit -v | Show the changes in the editor while you write the message |
git commit --amend -m "message" | Replace the last commit's message |
git commit --amend --no-edit | Add staged changes to the last commit and keep its message |
git log -1 | Show the newest commit in full |
Was this tutorial helpful?