0
0
Gitdevops~15 mins

Why staging before committing matters in Git - See It in Action

Choose your learning style9 modes available
Why staging before committing matters
📖 Scenario: You are working on a project with multiple files. You want to save your changes carefully so that only the right files are included in your commit history. This helps keep your project clean and organized.
🎯 Goal: Learn how to use git add to stage specific files before committing them with git commit. Understand why staging is important to control what goes into your commit.
📋 What You'll Learn
Create two new files with specific content
Stage only one file using git add
Commit the staged file with a message
Check the commit history to confirm only the staged file was committed
💡 Why This Matters
🌍 Real World
In real projects, you often change many files but want to commit only some of them. Staging lets you control exactly what goes into each commit, keeping your history clean and meaningful.
💼 Career
Understanding staging and committing is essential for collaborating in teams, managing code versions, and avoiding mistakes that can cause bugs or confusion.
Progress0 / 4 steps
1
Create two files with content
Create two files named file1.txt and file2.txt with the exact content: Hello from file1 and Hello from file2 respectively.
Git
Need a hint?

Use echo with redirection > to create files with content.

2
Stage only file1.txt
Use git add file1.txt to stage only file1.txt for the next commit.
Git
Need a hint?

Use git add with the exact filename file1.txt.

3
Commit the staged file
Use git commit -m "Add file1.txt only" to commit the staged file1.txt with the message exactly as shown.
Git
Need a hint?

Use git commit with the -m flag and the exact commit message.

4
Check commit history to confirm
Run git log --oneline and verify the latest commit message is Add file1.txt only. This confirms only file1.txt was committed.
Git
Need a hint?

The output of git log --oneline should show the commit message Add file1.txt only at the top.