Skip to content

Creating a Commit

A commit records a selected project state in local Git history. It uses the content in the Index, not every edit currently visible in your editor. This boundary lets you record completed work while leaving unfinished edits in the Working Tree.

last commit → new commit records Index content
│
Working Tree edits outside the Index remain outside the new commit

If you stage App/user.php and then edit it again, the commit records the staged version. The later edit remains in the Working Tree. After a successful commit, the staged difference has become history; it does not stay pending in the Index.

In a disposable PHP exercise, the staged diff showed a blank line and echo 99999999; added to App/user.php. The commit reported two insertions. A subsequent status check showed that a newer modification to the same file was still unstaged. This is why reviewing both git status and git diff --staged before committing is useful.

A commit represents the selected project-file state at a point in history. “Snapshot” does not mean Git stores a wholly separate physical copy of every unchanged file for each commit. Git can reuse previously stored, unchanged content. The detailed object model is covered later.

An ordinary commit has these essential fields:

Field Purpose
tree Links to the recorded file-and-directory state.
parent Links backward to the previous commit.
author Identifies who originally made the change.
committer Identifies who recorded this commit.
message Explains the purpose of the change.

The first commit in a repository is a root commit and has no parent. For ordinary later commits, the new commit points back to the previous one; the old one does not point forward to a future commit. Author and committer are normally the same in solo work, but can differ when work is applied or attributed by someone else. Merely receiving code does not automatically preserve the original author’s identity.

First inspect what the Index contains:

Terminal window
git status
git diff --staged

Then write a message directly:

Terminal window
git commit -m "Add user validation"

Or omit -m to write the message in your configured editor:

Terminal window
git commit

Git uses the effective author and committer identities from configuration and commit context, not automatically from a GitHub profile. Verify your Local identity before practicing if the repository contains placeholder settings.

Check the result:

Terminal window
git status
git log -1

If the Index has no selected difference, an ordinary commit has nothing to record even when the Working Tree has edits. Do not assume that a successful commit included all visible project changes.

git commit writes to the local repository data under .git. It does not upload anything to GitHub. A remote push transfers recorded commits later; it cannot send uncommitted Working Tree edits as commits. Remote operations are taught in their own phase.

  • Thinking editor Save and Git commit are the same action.
  • Assuming the latest Working Tree edit enters a commit without staging.
  • Expecting the staged difference to remain pending after a successful commit.
  • Imagining that every commit duplicates every unchanged file.
  • Assuming a local commit is already published online.
  • Forgetting to review staged content and identity before creating history.

In a backend project, inspect the Index for accidentally selected secrets or configuration changes. Do not rewrite an existing commit merely to practice; history-editing tools and their risks are covered later.

git commit -m "message" → record Index content with a supplied message
git commit → open the configured editor for a message
root commit → first commit, no parent
later ordinary commit → points back to its parent
unstaged edit → remains outside the new commit
Which version enters a commit after a file is edited again?

The version in the Index. The newer Working Tree edit remains outside.

Which field links a commit to its recorded file state?

The tree field.

Can a root commit have a parent, and does a commit upload to GitHub?

A root commit has no parent. A local commit does not upload anything.