Staging and Committing
💻 GitStage the right changes and write commits that explain themselves.
Git lets you choose exactly what goes into each commit. That control is what makes history readable.
- Stage files and parts of files.
- Write a clear commit message.
- Review a commit before saving it.
Stage Changes
Add specific files rather than everything when the changes are unrelated.
git add index.html
git add css/
# Stage every change
git add .
# Stage part of a file
git add -p styles.cssCommit
Write the message in the imperative mood: "Add login form", not "Added login form". The subject line should fit in about 50 characters.
git commit -m "Add login form validation"Review First
Use git diff --staged to see exactly what will be committed. This catches debug lines and secrets before they enter history.
git diff --stagedPractice
- Make two unrelated changes and commit them separately.
- Review a staged diff before committing.
- Rewrite a vague commit message into a clear one.
Hands-on exercise
Create a repository with two files.
1. Change the first file and commit it with a clear message.
2. Change the second file and commit it separately.
3. Run git log --oneline and confirm there are two commits.
Recap
Stage deliberately, review the diff and write messages that explain the reason for the change.
Example
git add index.html
git add css/
# Stage every change
git add .
# Stage part of a file
git add -p styles.css
Exercises
1. Change the first file and commit it with a clear message.
2. Change the second file and commit it separately.
3. Run git log --oneline and confirm there are two commits.
Expected Output
Three commits, each describing one change.
Lesson Nav
Git Course
0/9 lessons completed
- 1 Why Version Control Matters Tutorial
- 2 Your First Repository Tutorial
- 3 Staging and Committing Tutorial
- 4 History, Diffs and Logs Tutorial
- 5 Branching Tutorial
- 6 Merging and Resolving Conflicts Tutorial
- 7 Remotes and Collaboration Tutorial
- 8 Undoing Mistakes Safely Tutorial
- 9 Project: A Team Workflow Project