Your First Commit Workflow

Introduction

Add selects changes; commit saves a snapshot to history. This chapter walks through your first complete cycle—create files, stage them, commit with a clear message, and read the log—so every later Git operation builds on muscle memory.

Prerequisites

Start a Practice Repository

bash
mkdir git-practice
cd git-practice
git init

Create a simple project file:

bash
echo "# Git Practice" > README.md
mkdir src
echo "console.log('hello');" > src/index.js

Check state:

bash
git status

Both files appear under Untracked files.

Stage Changes with git add

Stage one file:

bash
git add README.md
git status

README.md moves to Changes to be committed; src/index.js stays untracked.

Stage remaining files:

bash
git add src/index.js

Or stage everything in the folder:

bash
git add .

Code explanation:

  • git add <path> copies the current file snapshot into the staging area
  • git add . stages all changes under current directory (respects .gitignore later)

Tip

Stage in Logical Chunks

Prefer two commits—README then code—over one giant git add . when changes are unrelated. Good habit for real projects.

Create the Commit

bash
git commit -m "Add README and hello script"

Success output similar to:

text
[main (root-commit) a1b2c3d] Add README and hello script
 2 files changed, 2 insertions(+)
 create mode 100644 README.md
 create mode 100644 src/index.js

Code explanation:

  • (root-commit) — first commit in repo has no parent
  • Hash a1b2c3d identifies this snapshot forever (full hash is longer)

Verify clean state:

bash
git status

Expected: nothing to commit, working tree clean.

Write Better Commit Messages

Format that scales to teams:

text
Short summary in imperative mood (50 chars or less)
 
Optional body: why the change, not how every line changed.
Wrap at ~72 characters.

Examples:

GoodWeak
Add user login formfixed stuff
Fix null check on empty emailupdate
Document API auth in READMEasdfasdf

Use imperative: “Add feature” not “Added feature”—matches generated messages.

Commit with Editor (No -m)

bash
git commit

Git opens your configured core.editor. Write message, save, close editor to finish. Abort by closing without saving or leaving empty message (Git cancels).

View History

bash
git log

Compact graph:

bash
git log --oneline --graph --decorate

Example:

text
* a1b2c3d (HEAD -> main) Add README and hello script

Code explanation:

  • HEAD -> main — you are on branch main at commit a1b2c3d
  • --oneline — one line per commit

Show latest commit details:

bash
git show

Second Commit (Edit Loop)

Change a file:

bash
echo "console.log('hello git');" > src/index.js
git status

Shows modified under Changes not staged.

Stage and commit:

bash
git add src/index.js
git commit -m "Update hello message in index.js"
git log --oneline

Two commits appear—history is a chain.

amend (Awareness Only)

Fix last commit message or include forgotten staged file:

bash
git commit --amend -m "Better message"

Warning

Do Not Amend Pushed Commits

--amend rewrites the last commit. Safe on local-only history; dangerous after push to shared branches—covered in undo chapter.

End-to-End Workflow Summary

bash
# 1. Edit files
# 2. Review
git status
# 3. Stage
git add <files>
# 4. Commit
git commit -m "Describe change"
# 5. Verify
git log --oneline

Repeat for every logical change.

Practice Checklist

  • git init created repo
  • At least two commits with clear messages
  • git log --oneline shows both
  • git status clean after commit
  • Author shows your configured name/email in git log

FAQ

Committed but forgot a file?

Stage the file and run git commit --amend locally, or make a second commit—prefer second commit if already pushed.

git add . staged too much?

git restore --staged <file> to unstage—undo chapter details.

Empty commit?

git commit --allow-empty -m "Trigger CI"—rare; normal commits need staged changes.

Who can see commits?

Only you until you push to a remote—next chapters.

Binary files?

Git commits binaries but diffs poorly—images and jars work; use .gitignore for build artifacts.

Commit frequency?

Small, frequent commits beat rare huge dumps—easier review and revert.