TExcelTAD
Home › Git › Git Commit

Git Commit

A commit is a saved snapshot of the staged files, together with metadata: author, date, a message, and a pointer to the previous commit (its parent). Commits are the points in history you can return to.

Your first commit

bash · try it yourself
mkdir journal && cd journal
git init -q
printf 'Day 1: started learning Git\n' > diary.txt
git add diary.txt
git commit -m "Start the diary"
git status
[main (root-commit) 6609eb1] Start the diary
 1 file changed, 1 insertion(+)
 create mode 100644 diary.txt
On branch main
nothing to commit, working tree clean

The first line of the commit output tells you the branch (main), that this commit has no parent (root-commit), its short hash and its message. Afterwards the working tree is clean: nothing differs from the last commit.

The commit hash

Every commit is identified by a 40-character SHA-1 hash computed from its content, its parent, the author, the date and the message. Change any of them and the hash changes. You almost always use the first 7 characters:

bash · try it yourself
mkdir journal && cd journal
git init -q
echo "Day 1" > diary.txt && git add diary.txt && git commit -q -m "Day 1"
git rev-parse HEAD
git rev-parse --short HEAD
git log -1 --format='%H%n%an <%ae>%n%ad%n%s'
dfb708743709ae2e34b42de02d59328992e1e1ad
dfb7087
dfb708743709ae2e34b42de02d59328992e1e1ad
Ada Lovelace <ada@example.com>
Thu Jan 15 10:00:00 2026 +0000
Day 1

HEAD means "the commit you currently have checked out".

Writing good messages

The message explains why the change was made; the diff already shows what changed. A widely used convention:

  • A short summary line (about 50 characters), in the imperative: "Add search box", not "Added" or "Adds".
  • A blank line, then an optional body that explains the reasoning, wrapped at about 72 characters.

You can give several -m options; each becomes a separate paragraph:

bash · try it yourself
mkdir journal && cd journal
git init -q
echo "Day 1" > diary.txt && git add diary.txt
git commit -q -m "Add first diary entry" -m "Keeping a diary helps me remember what I learned each day."
git log
commit 78f9aa0ceef2d532607c5fcf00c0efd4df5cf629
Author: Ada Lovelace <ada@example.com>
Date:   Thu Jan 15 10:00:00 2026 +0000

    Add first diary entry

    Keeping a diary helps me remember what I learned each day.

Without -m, Git opens your editor (the core.editor setting) so you can write a longer message. Lines starting with # in the editor are comments and are removed.

Committing tracked changes directly

git commit -a stages every modification and deletion of tracked files, then commits, in one step. New files still need git add:

bash · try it yourself
mkdir journal && cd journal
git init -q
echo "Day 1" > diary.txt && git add diary.txt && git commit -q -m "Day 1"
echo "Day 2" >> diary.txt
echo "todo" > todo.txt
git commit -a -m "Day 2"
git status -s
[main ee2fcb2] Day 2
 1 file changed, 1 insertion(+)
?? todo.txt

The diary change was committed, but the new todo.txt is still untracked. Use -a carefully: it is easy to commit an edit you didn't mean to.

Looking at commits

bash · try it yourself
mkdir journal && cd journal
git init -q
echo "Day 1" > diary.txt && git add diary.txt && git commit -q -m "Day 1"
echo "Day 2" >> diary.txt && git commit -q -am "Day 2"
git log --oneline
echo "--- what changed in the latest commit:"
git show --stat --format='%h %s' HEAD
ee2fcb2 Day 2
dfb7087 Day 1
--- what changed in the latest commit:
ee2fcb2 Day 2

 diary.txt | 1 +
 1 file changed, 1 insertion(+)

Tip: Commit small and often. A commit that does one thing is easy to review, easy to revert and has an easy message to write.

Test yourself

What does git commit -a -m "Fix" do with a brand-new, untracked file?

  1. Commits it
  2. Leaves it untracked
  3. Deletes it
  4. Adds it to .gitignore

Which is the recommended style for a commit summary line?

  1. fixed stuff
  2. Add password strength meter to sign-up form
  3. Changes made on Tuesday to several files in the project folder
  4. WIP

Which of these does NOT affect a commit's hash?

  1. The commit message
  2. The parent commit
  3. The file contents
  4. The name of the folder that contains the repository