Managing Files

Loading concept...

Daily Git Workflow: Managing Files Like a Pro 🎒

The Messy Backpack Story

Imagine your school backpack. You put your homework, books, snacks, and toys inside. But wait—some things you never want to bring to school, like your messy candy wrappers or secret diary!

Git works the same way. Your project folder is like a backpack. Git tracks everything inside. But sometimes, you have files you don’t want Git to see—like passwords, temporary files, or that huge video you’re editing.

Let’s learn how to tell Git: “Hey, ignore this stuff!”


1. Ignoring Files with .gitignore 🙈

What Is It?

A .gitignore file is like a “Do Not Touch” list for Git.

Think of it as a note on your backpack saying:

“Dear Git, please pretend these items don’t exist.”

How to Create One

# Create a .gitignore file
touch .gitignore

Then open it and write the names of files or folders you want Git to ignore.

Simple Example

# My .gitignore file

secrets.txt
my-diary.md
snacks/

Now Git will never track secrets.txt, my-diary.md, or anything inside the snacks/ folder!

Why Does This Matter?

  • Keeps secrets safe (passwords, API keys)
  • Saves space (no huge files in your repo)
  • Stays clean (no junk files)

2. Gitignore Patterns: The Magic Rules ✨

The .gitignore file understands special patterns—like magic spells that match many files at once!

The Pattern Cheat Guide

Pattern What It Means Example
*.log Any file ending in .log error.log, debug.log
build/ The whole build folder Everything inside build/
!important.log Don’t ignore this one Keeps important.log even if *.log is ignored
**/temp temp folder anywhere src/temp, lib/temp
doc/*.pdf PDFs only in doc/ folder doc/guide.pdf

Real-World Example

# Ignore all .log files
*.log

# Ignore node_modules folder
node_modules/

# Ignore all .env files (secrets!)
.env
.env.local

# But keep .env.example
!.env.example

# Ignore temp folders anywhere
**/temp/

The Story Behind **

Imagine you have folders inside folders inside folders. The ** pattern says: “Find this anywhere, no matter how deep!”

project/
├── temp/          ← **/temp catches this
├── src/
│   └── temp/      ← and this
└── lib/
    └── utils/
        └── temp/  ← and this too!

3. Ignoring Already Tracked Files 😅

The Problem

Oops! You forgot to add .gitignore at the start. Git is already tracking secrets.txt. Now what?

Just adding it to .gitignore won’t work. Git remembers files it already knows!

The Solution: Remove from Memory

# Tell Git to forget the file
# (but keep it on your computer!)
git rm --cached secrets.txt

Then add to .gitignore:

secrets.txt

Finally, commit the change:

git add .gitignore
git commit -m "Stop tracking secrets.txt"

For a Whole Folder

git rm -r --cached node_modules/

The -r means “recursive”—it removes everything inside.

What --cached Does

Command What Happens
git rm file.txt Deletes from Git AND your computer
git rm --cached file.txt Removes from Git ONLY (file stays safe!)

Think of --cached as saying: “Forget you ever saw this, but don’t throw it away!”


4. Removing Files from Git 🗑️

Sometimes you want to completely delete a file—from Git AND your computer.

Delete and Tell Git

# Remove the file completely
git rm old-notes.txt

# Commit the removal
git commit -m "Delete old-notes.txt"

What Actually Happens

graph TD A[File exists in Git + Computer] --> B[git rm file.txt] B --> C[File deleted from computer] B --> D[Git stages the deletion] D --> E[git commit] E --> F[File removed from Git history going forward]

Force Remove

If Git complains the file has changes:

git rm -f stubborn-file.txt

The -f means “force”—do it anyway!


5. Moving and Renaming Files 📦

Git is smart about tracking files, even when you move or rename them!

Renaming a File

# Old name → New name
git mv old-name.txt new-name.txt

# Commit the rename
git commit -m "Rename old-name to new-name"

Moving a File

# Move file to a folder
git mv report.txt docs/report.txt

# Commit the move
git commit -m "Move report to docs folder"

What git mv Actually Does

It’s a shortcut for three steps:

# The long way:
mv old.txt new.txt      # 1. Rename the file
git rm old.txt          # 2. Tell Git old name is gone
git add new.txt         # 3. Tell Git about new name

# The short way:
git mv old.txt new.txt  # Does all 3 at once!

Why Use git mv?

Method Git Understands Rename?
Regular mv Sometimes (has to guess)
git mv Always (you told it!)

Git keeps the file’s history when you use git mv. It knows the file just got a new name, not that you deleted one and created another!


Quick Command Reference

# Create .gitignore
touch .gitignore

# Stop tracking a file (keep on computer)
git rm --cached filename

# Delete file completely
git rm filename

# Rename/move a file
git mv oldname newname

The Golden Rules 📜

  1. Create .gitignore first — before your first commit!
  2. Use patterns — don’t list every single file
  3. Use --cached — when you want Git to forget, not delete
  4. Use git mv — to keep your file history clean

You Did It! 🎉

Now you know how to:

  • Tell Git which files to ignore
  • Use wildcard patterns like a wizard
  • Remove files Git already knows about
  • Delete files properly
  • Move and rename without losing history

Your Git backpack is now perfectly organized. No more messy candy wrappers in your code! 🎒✨

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.