==========================
🧠 Git Basics & Workflow
==========================
Git is a distributed version control system used to track changes in code. It works
using three main areas:
1. Working Directory – your actual files/folders
2. Staging Area – temporary area where files are prepared for commit
3. Repository – permanent storage of committed changes
--------------------------------------------
🗂 Git Areas: How Files Flow
--------------------------------------------
Working Directory --> (git add) --> Staging Area --> (git commit) --> Repository
--------------------------------------------
🔁 Git Workflow (Step-by-Step)
--------------------------------------------
1. git init # Initialize Git in the folder
2. git add <file> # Stage a file for commit
3. git commit -m "msg" # Commit the staged changes
4. git status # Show current state of files
5. git log # Show commit history
6. git push / git pull # Send or get changes from remote repo
--------------------------------------------
🧾 Tracked vs Untracked Files
--------------------------------------------
- Untracked = Git doesn't watch this file yet
- Tracked = Git is tracking changes (after `git add`)
- Modified = Changed but not staged
- Staged = Ready for commit
- Committed = Saved in Git history
--------------------------------------------
🧰 Useful Git Commands
--------------------------------------------
git add . # Stage all files
git add -p # Stage part of a file
git commit -m "msg" # Commit with message
git reset <file> # Unstage a file
git diff # Show unstaged changes
git diff --staged # Show staged changes
git rm --cached <file> # Stop tracking a file
git log # See commit history
git show <commit-id> # View a specific commit
git checkout -- <file> # Discard changes to a file
--------------------------------------------
🔗 Remote GitHub Flow
--------------------------------------------
git remote add origin <url> # Connect to remote repo
git branch -M main # Set main branch
git push -u origin main # Push initial code
git pull origin main # Pull latest changes
--------------------------------------------
💡 Key Concept Summary
--------------------------------------------
- git add = Move to staging area
- git commit = Save snapshot to repo
- After commit, files stay in both working directory AND Git history
- Only staged files are committed
--------------------------------------------
🔄 Undo & Recovery Commands in Git
--------------------------------------------
| Action | Command |
What It Does |
|----------------------------------------|----------------------------------------|
------------------------------------------------------------|
| Unstage a file | git reset <file> |
Moves file from staging back to working directory |
| Amend last commit | git commit --amend |
Edit last commit message or add new staged files to it |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
Undo commit, keep all changes staged |
| Undo last commit (unstage too) | git reset --mixed HEAD~1 |
Undo commit, unstage changes |
| Undo last commit (erase changes) | git reset --hard HEAD~1 |
Completely undo last commit and remove changes |
| Discard changes in a file | git checkout -- <file> |
Restore file from last commit |
| Discard all local changes (DANGEROUS) | git reset --hard |
Reset all files to last commit, erases unsaved work |
| Stop tracking a file but keep it | git rm --cached <file> |
Remove from Git, keep file in project folder |
⚠️ Be cautious with `--hard` resets; they can permanently delete uncommitted
changes.