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.
1. What is recorded?
Section titled “1. What is recorded?”last commit → new commit records Index content │Working Tree edits outside the Index remain outside the new commitIf 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.
2. Snapshot mental model
Section titled “2. Snapshot mental model”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.
3. Create a commit
Section titled “3. Create a commit”First inspect what the Index contains:
git statusgit diff --stagedThen write a message directly:
git commit -m "Add user validation"Or omit -m to write the message in your configured editor:
git commitGit 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:
git statusgit log -1If 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.
4. Local versus remote
Section titled “4. Local versus remote”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.
5. Common mistakes and safety
Section titled “5. Common mistakes and safety”- 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.
6. Summary
Section titled “6. Summary”git commit -m "message" → record Index content with a supplied messagegit commit → open the configured editor for a messageroot commit → first commit, no parentlater ordinary commit → points back to its parentunstaged edit → remains outside the new commit7. Knowledge check
Section titled “7. Knowledge check”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.