Git Commit

A commit is a snapshot of your project at one moment. The git commit command takes everything in the staging area and saves it in the repository with a message. Every commit has a unique ID (a hash), the author, the date and the message.

Syntax

git commit -m "message"
git commit -a -m "message"
git commit
git commit --amend

Make a commit

Terminal output

$ mkdir notes
$ cd notes
$ git init -q
$ echo "Buy milk" > todo.txt
$ git add todo.txt
$ git commit -m "Add todo list"
[main (root-commit) 9dd148c] Add todo list
1 file changed, 1 insertion(+)
create mode 100644 todo.txt

The output has the branch name, the short hash, the message, and a small summary of the changes. The root-commit word is shown only for the very first commit of a repository.

Commit without adding (the -a option)

The option -a stages all the modified and deleted tracked files automatically, and then commits. It does not take new (untracked) files.

Terminal output

$ echo "Buy eggs" >> todo.txt
$ git commit -a -m "Add eggs to the list"
[main a029f0b] Add eggs to the list
1 file changed, 1 insertion(+)

When nothing is staged

Terminal output

$ git commit -m "Try again"
On branch main
nothing to commit, working tree clean

Git does not create a commit when there is no change. It tells you what to do.

Commit with a long message

If you run git commit without -m, Git opens your editor. The first line is the short title, then a blank line, and then the longer description. You can also use -m two times:

Terminal output

$ echo "Buy bread" >> todo.txt
$ git commit -a -m "Add bread to the list" -m "Bread is needed for the breakfast on Monday."
[main af5b94c] Add bread to the list
1 file changed, 1 insertion(+)
$ git log -1
commit af5b94ca56499e5f2cdeb5ac0fd1554c00bea1aa (HEAD -> main)
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 11:17:00 2026 +0000

Add bread to the list

Bread is needed for the breakfast on Monday.

How to write a good commit message

  • Write the title in one short line (50 characters or less is a good rule).
  • Start with a verb: “Add”, “Fix”, “Remove”, “Update”, not “Added” or “Fixed”.
  • Say what the change does and why, not how.
  • One commit should do one thing. Do not mix a bug fix and a new feature.
Bad messageBetter message
fixFix login error when the password is empty
changesUpdate footer links on the contact page
work in progressAdd search box to the header

Empty commit

Sometimes you need a commit with no file changes, for example to trigger a build on the server. Use the --allow-empty option.

Terminal output

$ git commit –allow-empty -m "Trigger build"
[main 67a8509] Trigger build

Note: A commit is local until you push it. If you made a mistake in the last commit, you can fix it with git commit –amend. To see the history, go to Git Log.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.