New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Git› Staging and Committing

Staging and Committing

💻 Git

Stage the right changes and write commits that explain themselves.

Lesson 3 of 9 Tutorial
0 of 9 completed

Git lets you choose exactly what goes into each commit. That control is what makes history readable.

By the end of this lesson you can:
  • 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.css

Commit

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 --staged
Tip A commit message should explain why the change was made. The diff already shows what changed.

Practice

  1. Make two unrelated changes and commit them separately.
  2. Review a staged diff before committing.
  3. 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

Exercise 1. Two clean commits Easy
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.
Git exercise-1.txt
Expected Output
Three commits, each describing one change.

Lesson Nav

Lesson sections