Skip to content
jsworkbench.
Learning path
Foundations18 min

Commits

Understand the idea

A commit is a snapshot of the files plus a pointer to its parent, not a list of edits.

The idea

The stores every tracked file as it was, plus the parent hashes. git show and git diff compute the edit by comparing two photographs. That is why a merge commit can have two parents: two photographs both led to this one.

What changes

git add copies the chosen files from the into the . git commit -m "message" freezes that index, points it at the current HEAD, and moves the branch label forward. git commit --amend -m "message" replaces the tip you have not shared by writing a new hash with the same parent.

What a commit is made of
PartRoleWhere you see it
SnapshotThe files at that momentgit show
ParentsThe commits this one followsgit log --graph
MessageWhy this snapshot existsgit log --oneline

The command

write README.md first draft, then git add README.md, then git commit -m "describe the project". Run git log --oneline --graph. The new node sits above the initial commit, and main points at it. Amend only if this commit is still only yours.

The mistake

Check your graph

You should see two commits, the new one parented on the initial commit, with HEAD on main. git show HEAD prints the snapshot you staged, not a file you forgot to add.

Your repository

write a file, git add it, and git commit. Each commit appears on the graph of this repository.

  1. Step 1

    Change the snapshot

    In the editor, open README.md and add a second line: What this project is for

  2. Step 2

    Compare with the last commit

    git diff shows the working tree against the index. The last commit is unchanged until you stage and commit.

  3. Step 3

    Stage only that file

    The index becomes the next snapshot. Unstaged edits stay out.

  4. Step 4

    Check what will be stored

    The staged diff is the change the commit will freeze.

  5. Step 5

    Commit with a reason

    The message says why. The graph grows one node, and main moves to it.

  6. Step 6

    Read the stored snapshot

    git show prints the commit you just made, not the editor.

The editor edits the working tree. Start a repository and the files appear here.

Follow the steps. Each command step can be placed in the terminal. Edit files in the editor, then stage them.
Supported command reference
  • init
  • add <path> | add .
  • commit -m "message" | commit --amend -m "message"
  • status
  • diff | diff --staged
  • log | log --oneline | log --graph | log --oneline --graph
  • show [ref]
  • branch | branch <name> | branch -d <name>
  • checkout <ref> | checkout -b <name> | switch <name> | switch -c <name>
  • merge <branch>
  • rebase <branch> | rebase -i <branch> | rebase --edit-todo <pick|drop|reword> <hash> [message] | rebase --continue | rebase --abort
  • cherry-pick <ref>
  • reset --soft <ref> | reset --mixed <ref> | reset --hard <ref>
  • revert <ref>
  • stash | stash list | stash pop | stash apply
  • restore <path> | restore --staged <path>
  • remote | remote add <name> <repo-slug>
  • clone <repo-slug> | git clone <repo-slug> | git fork <repo-slug>
  • fetch [remote] | pull [remote] [branch] | push [remote] [branch] | push --force [remote] [branch]
  • blame <path>
  • reflog
  • tag | tag <name> [ref] | tag -d <name>
  • bisect start | bisect good [ref] | bisect bad [ref] | bisect reset
  • config <key> <value> | config --list
  • write <path> <text> (workbench: edit the working tree)
  • issue new "title" | pr new <source> <target> "title" | review "comment" | review --request-changes "comment"

Commit graph

No commits yet. git init creates the first one.

Quiz

A quick check for understanding. Retry as often as you like; your learning path stays open.

01What does a commit store?
02What moves forward when you commit on a branch?

Ready to call this one understood?

Mark this lesson complete to earn 100 XP. You can always revisit it.

Sign in to save progress
Next lesson