Staging & Committing Changes in Git – A Beginner’s Guide

Why Staging and Committing Matter?

Git uses a two-step process to track changes:

1️⃣ Staging – Prepares changes to be included in the next commit.
2️⃣ Committing – Saves those changes as a snapshot in the repository.

This allows developers to review changes before committing and create meaningful commit histories for better version control.


Checking the Status of Your Changes

Before staging or committing, check what has changed in your repository:

git status

This command shows: ✅ New (untracked) files.
✅ Modified files.
✅ Staged files ready for commit.


Staging Changes

Stage a Single File

To include a specific file in the next commit:

git add <file-name>

Example:

git add index.html

Stage Multiple Files

To stage multiple specific files:

git add file1.txt file2.txt

Stage All Modified Files

To stage all modified and new files at once:

git add .

Stage Files Interactively

If you want to review and selectively stage parts of files, use interactive mode:

git add -p

This will present changes in chunks and ask if you want to stage them individually.


Committing Changes

Once files are staged, save them as a commit:

git commit -m "Your commit message here"

Example:

git commit -m "Updated navigation bar"

Commit messages should be clear and descriptive to help track project history.

To edit the most recent commit message:

git commit --amend -m "New commit message"

Viewing Commit History

To see past commits:

git log

For a simplified view:

git log --oneline

To see commit history for a specific file:

git log -- <file-name>

Removing Files from Staging

If you added a file but don’t want to commit it yet:

git reset <file-name>

To unstage all changes:

git reset

To remove staged changes from a file while keeping modifications:

git restore --staged <file-name>

Next Steps

Now that you know how to stage and commit changes, explore more Git topics:

Undoing Mistakes in Git – Learn how to revert and reset changes.
Pushing & Pulling from GitHub & GitLab – Sync local changes with remote repositories.

Need help? Check the official Git documentation or ask in the comments!