CHAPTER 7: Committing, Skipping Staging, Removing & Moving Files

Commit the Changes We've Made

First, we stage changes using: git add <filename> or git add .(for all changes).

Then we commit them with: git commit -m "your commit message"

graph LR A[Working Directory] --> B[Staging Area] B --> C[Repository] A -->|git add| B B -->|git commit| C
file1.txt
file2.js

Commits:

What If I Skip Staging? 🀨

You can use git commit -am "message" to skip staging.

πŸ—‘οΈ Removing Files in Git

Sometimes, you need to remove files from both:

Use this command:

git rm <filename>

🎯 Example:

git rm old_data.csv

This command:

  1. ❌ Deletes old_data.csv from your folder
  2. πŸ“‹ Stages its removal so the deletion is included in your next commit

πŸ€” What if I delete the file manually (outside Git)?

Git will notice it’s missing and show this in git status:

deleted: old_data.csv

But it won’t be staged until you run:

git add -u

🚫 Want to keep the file but stop tracking it?

Use this command to untrack the file without deleting it from disk:

git rm --cached <filename>

This is super useful for removing sensitive or unnecessary files like logs, build folders, or API keys.

πŸ’‘ Tip:

Once removed, add that file to .gitignore to prevent future accidental tracking.

🚚 Moving or Renaming Files in Git

Renaming or moving a file? Git has a special command:

git mv <old_filename> <new_filename>

πŸ“¦ Example:

git mv notes.txt docs/meeting-notes.md

This command:

  1. πŸ” Renames or moves the file in your folder
  2. πŸ“ Automatically stages the change for the next commit

🧠 Why use git mv instead of renaming manually?

If you rename manually (via File Explorer or command line), Git treats it like:

This can clutter your history with unnecessary deletions and additions.

πŸ•΅οΈβ€β™‚οΈ Fun Fact:

Git uses smart algorithms to detect renamesβ€”even if you move manually! If content remains mostly the same, Git will show it as a rename in git diff or git status.

But using git mv makes your intent clear and keeps your commits cleaner.

🧠 Quick Quiz: Test Your Understanding!

What command will commit only modified (already tracked) files without staging first?