Staging and Committing in Git
Git tracks changes using a three-step workflow:
- Modify your files
- Add them to the staging area
- Commit them to the repository
Git Workflow
Edit → Stage → Commit
- Edit: Make changes in files
- Stage: Mark them for versioning
- Commit: Save the snapshot
Staging vs Committing
Stage | Description |
---|---|
git add | Adds changes to the staging area |
git commit | Takes a snapshot of staged changes |
Add Files to Staging Area
Single File
git add README.md
All Files in Directory
git add .
.
= All files in current and subdirectories
Commit the Files
git commit -m "Adding a README."
Expected Output:
[main cb33c18] Adding a README.
1 file changed, 1 insertion(+)
create mode 100644 README.md
💡 Use clear, short commit messages for each change
Best Practices for Commits
- ✅ Use
-m
for one-line commit messages - 📄 Save detailed notes using
git commit
(opens editor) - ⏪ Commits are snapshots of your work
Try It Yourself
Problem 1: Add and Commit One File
Show Code
touch hello.txt
git add hello.txt
git commit -m "Add hello.txt"
Problem 2: Add All and Commit
Show Code
git add .
git commit -m "Initial commit with all files"