SovranCode
HomeCourses Git & GitHub Git Commit
This device
Course contentsGit Commit · 41 topics

1. Git Fundamentals

Git IntroductionGit Version ControlGit vs GitHubGit InstallationGit Working TreeGit RepositoryGit CommitGit .gitignoreGit Project: Learning Journal

2. Git History

Git LogGit DiffGit ResetGit RevertGit StashGit Project: Messy Repository

3. Branches & History

Git BranchGit MergeGit Merge ConflictsGit RebaseGit Cherry-PickGit TagsGit Project: Feature Branch

4. Remotes and GitHub

Git RemoteGitHub AuthenticationGitHub RepositoryGitHub ForkGitHub IssuesGitHub PagesGit Project: Two Clone Sync

5. GitHub Collaboration

GitHub Pull RequestsGitHub Code ReviewGit Branching WorkflowGitHub Branch ProtectionGitHub CollaborationGitHub ContributingGit Project: Reviewed Pull Request

6. Automation and Professional Git

GitHub ActionsGit InternalsGit RecoveryGitHub SecurityGit Project: Team Repository CIProject
Learn Git & GitHub40 complete · 1 planned

1. Git Fundamentals

Git IntroductionGit Version ControlGit vs GitHubGit InstallationGit Working TreeGit RepositoryGit CommitGit .gitignoreGit Project: Learning Journal

2. Git History

Git LogGit DiffGit ResetGit RevertGit StashGit Project: Messy Repository

3. Branches & History

Git BranchGit MergeGit Merge ConflictsGit RebaseGit Cherry-PickGit TagsGit Project: Feature Branch

4. Remotes and GitHub

Git RemoteGitHub AuthenticationGitHub RepositoryGitHub ForkGitHub IssuesGitHub PagesGit Project: Two Clone Sync

5. GitHub Collaboration

GitHub Pull RequestsGitHub Code ReviewGit Branching WorkflowGitHub Branch ProtectionGitHub CollaborationGitHub ContributingGit Project: Reviewed Pull Request

6. Automation and Professional Git

GitHub ActionsGit InternalsGit RecoveryGitHub SecurityGit Project: Team Repository CIProject
PREVIOUS LESSONGit Repository
NEXT LESSONGit .gitignore
1. Git Fundamentals 40 min

Git Commit

Write a useful commit message, record a root commit from the index, and prove the snapshot with log, show, and status.

What you will leave with

You will write a commit message that states completed intent, record a root commit from a reviewed index, prove the snapshot with log and show, and explain why later edits do not rewrite HEAD.

A commit is a recorded snapshot

The previous lesson prepared a Git repository and staged exact files. This lesson records that proposal as history. git commit does not “save files” in the editor sense, and it does not upload anything to GitHub. It writes a snapshot of the index, with your identity and message, as a commit object.

Continue in hello-history from Git Repository, or recreate that state: one README staged, nothing else selected.

Git, not GitHub

Until a remote exists, every commit lives only on this machine. Hosting comes later. The command is a Git command.

Write a useful commit message

A commit message explains why this complete state belongs in history. For the first snapshot, Create project README is specific, short, and written as an imperative action.

Weak

“changes”

  • Does not identify the outcome.
  • Cannot distinguish this commit later.
  • Forces the reader to reconstruct intent from the diff.
Useful

“Create project README”

  • Names the completed outcome.
  • Matches the staged content.
  • Fits naturally after “This commit will…”
Message test

Complete the sentence “This commit will…” If the subject forms a clear action—“Create project README”—it usually reads well in logs and release notes.

QUICK CHECK

Test what you learned

Which commit subject best describes a snapshot that adds a project README?

Create the root commit

git status
git diff --cached

git commit -m "Create project README"

git status
git log --oneline --decorate -1

If the commit succeeds, Git writes a tree from the index, creates a commit object with your configured identity and message, and updates the current branch to point to it. Because no earlier commit exists, this is a root commit with no parent.

The commit output normally includes the branch name, an abbreviated object identifier, the subject, and a file summary. Read it. Command output is evidence, not decoration.

Do not use -a yet

git commit -a stages modifications and deletions of tracked files automatically, but not untracked files. It bypasses the explicit selection step taught in Git Repository.

Prove the snapshot exists

git status
git log --oneline --decorate -1
git show --stat --oneline HEAD
git rev-parse --show-toplevel
git ls-tree --name-only HEAD
  1. STATUS
    Working tree is clean

    No index or working differences remain after the commit—assuming you did not edit again.

  2. LOG
    The branch points to one commit

    The one-line log shows an abbreviated identifier and your commit subject.

  3. SHOW
    HEAD has the expected summary

    The commit metadata and file statistics match the snapshot you intended.

  4. TREE
    README belongs to the committed tree

    ls-tree proves the path exists in HEAD, independent of the working file.

A clean status does not mean “Git has no files.” It means the working tree and index match the snapshot at HEAD, and there are no reportable untracked paths. Git Log later teaches richer history reading.

Make one edit after the commit

Add a second sentence to README and run git status, git diff, and git diff --cached. Predict the output first:

Status

README is modified but not staged because working content differs from the index.

Plain diff

Shows the new working-tree sentence relative to the index.

Cached diff

Is empty because index and HEAD still match after the previous commit.

HEAD

Still contains the original README snapshot. Editing does not rewrite history.

Leave this edit uncommitted or restore it manually by matching the committed text. The goal is to prove the three-place model after a real commit, not to accumulate unexplained history.

Independent lab: a verifiable root commit

Create a disposable folder named first-snapshot-lab. Add two files: README.md describing the project and notes.txt with one learning goal. Then:

  1. Initialize the repository if it is not already a Git repository.
  2. Stage only README.md and prove notes.txt remains untracked.
  3. Review the cached diff, then commit only the README with a specific message such as Create lab README.
  4. Use log, show, and ls-tree to prove what the root commit contains.
  5. Explain why status is not clean even though the commit succeeded.
Definition of done

Your root commit contains exactly README. notes.txt remains untracked, and you can explain that a successful commit need not produce a clean status when intentionally untracked work remains.

Common commit mistakes

Committing without reviewing the index

Run git diff --cached so the message matches the snapshot.

Writing “update” as the subject

State the completed outcome so later git log is readable.

Assuming commit means GitHub

The commit exists locally. No remote has been configured or contacted.

Trusting “success” without proof

Use status, log, show, and tree inspection to verify the resulting state.

Lesson review

You recorded a root commit from a reviewed index, wrote a message that states intent, and proved the snapshot independently of the working file. Later edits stay in the working tree until you stage and commit again.

  • I know git commit records the index, not unsaved editor buffers or GitHub.
  • I can write a commit subject that states the snapshot’s purpose.
  • I can prove the root commit with status, log, show, and tree inspection.
  • I can explain why editing a file after commit does not change HEAD.

Related lessons

  • Git Repository — Stage the exact files the commit should record.
  • Git Log — Read the history that commits create.
KNOWLEDGE CHECK

Check the Git commit model

A commit is a recorded snapshot of the index, not a save button and not a GitHub upload.

01What does git commit record?
02What makes the first commit a root commit?
03Which commit subject is most useful?
04After committing staged README v1, you edit the file to v2. What does HEAD still contain?
05Why is git commit -a a poor first habit?
PREVIOUS LESSONGit Repository
NEXT LESSONGit .gitignore
ON THIS PAGEGit CommitA commit is a recorded snapshotWrite a useful commit messageCreate the root commitProve the snapshot existsMake one edit after the commitIndependent lab: a verifiable root commitCommon commit mistakesLesson reviewKnowledge checkRelated lessons
Course contents