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
- Repositories and the Working Tree
- Git Configuration with
user.nameanduser.emailset
Start a Practice Repository
mkdir git-practice
cd git-practice
git initCreate a simple project file:
echo "# Git Practice" > README.md
mkdir src
echo "console.log('hello');" > src/index.jsCheck state:
git statusBoth files appear under Untracked files.
Stage Changes with git add
Stage one file:
git add README.md
git statusREADME.md moves to Changes to be committed; src/index.js stays untracked.
Stage remaining files:
git add src/index.jsOr stage everything in the folder:
git add .Code explanation:
git add <path>copies the current file snapshot into the staging areagit add .stages all changes under current directory (respects.gitignorelater)
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
git commit -m "Add README and hello script"Success output similar to:
[main (root-commit) a1b2c3d] Add README and hello script
2 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 src/index.jsCode explanation:
(root-commit)— first commit in repo has no parent- Hash
a1b2c3didentifies this snapshot forever (full hash is longer)
Verify clean state:
git statusExpected: nothing to commit, working tree clean.
Write Better Commit Messages
Format that scales to teams:
Short summary in imperative mood (50 chars or less)
Optional body: why the change, not how every line changed.
Wrap at ~72 characters.Examples:
| Good | Weak |
|---|---|
Add user login form | fixed stuff |
Fix null check on empty email | update |
Document API auth in README | asdfasdf |
Use imperative: “Add feature” not “Added feature”—matches generated messages.
Commit with Editor (No -m)
git commitGit 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
git logCompact graph:
git log --oneline --graph --decorateExample:
* a1b2c3d (HEAD -> main) Add README and hello scriptCode explanation:
HEAD -> main— you are on branchmainat commita1b2c3d--oneline— one line per commit
Show latest commit details:
git showSecond Commit (Edit Loop)
Change a file:
echo "console.log('hello git');" > src/index.js
git statusShows modified under Changes not staged.
Stage and commit:
git add src/index.js
git commit -m "Update hello message in index.js"
git log --onelineTwo commits appear—history is a chain.
amend (Awareness Only)
Fix last commit message or include forgotten staged file:
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
# 1. Edit files
# 2. Review
git status
# 3. Stage
git add <files>
# 4. Commit
git commit -m "Describe change"
# 5. Verify
git log --onelineRepeat for every logical change.
Practice Checklist
-
git initcreated repo - At least two commits with clear messages
-
git log --onelineshows both -
git statusclean 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.