← Back to Chapters

Git Staging Area

? Git Staging Area

? Quick Overview

In this section, we will dive into the staging area in Git. The staging area is where you prepare changes to be committed to the repository. It's an essential part of Git's workflow and allows you to control exactly what changes are included in each commit.

? Key Concepts

  • Staging area is also called the index
  • It sits between working directory and repository
  • Allows selective commits
  • Improves commit clarity and control

? What is the Staging Area?

The staging area, also known as the index, is a space where Git gathers changes before committing them. It acts as a middle ground between your working directory and the repository.

? Checking Repository Status

? View Code Example
// Shows modified, staged, and untracked files
git status

➕ Adding Files to Staging

? View Code Example
// Stages a specific file
git add file-name
? View Code Example
// Stages all files in current directory
git add .

➖ Removing Files from Staging

? View Code Example
// Unstages a file but keeps changes
git reset file-name

? Staging Partial Changes

? View Code Example
// Interactively stage parts of a file
git add -p file-name

? Viewing Staged Changes

? View Code Example
// Shows differences between staged files and last commit
git diff --cached

✅ Committing Staged Changes

? View Code Example
// Commits staged changes with a message
git commit -m "Commit message"

? Use Cases

  • Commit only tested changes
  • Split large changes into logical commits
  • Review changes before committing

? Tips & Best Practices

  • Stage related changes together
  • Review staged changes before commit
  • Use descriptive commit messages

? Try It Yourself

  • Modify a file and run git status
  • Stage using git add
  • Unstage with git reset
  • Commit and view history using git log