GIT & GITHUB SURVIVAL WALL // Andy's Edition v1.0
Version Control • Collaboration • Branching • Disaster Recovery
1. THE MENTAL MODEL
Git has 4 isolated zones. You must move files between them explicitly.
WORKING DIR STAGING LOCAL REPO
(Your Folder) (The Bus) (The Vault)
│ │ │
├─git add───>│ │
│ │ │
│ ├─commit───>│
│ │ │
│<─restore───┤ │
│ │ │
│<─────reset --hard─────┤
+ REMOTE REPO (GitHub) via push/pull
2. START & CLONE
git clone
git clone git@github.com:user/repo.git
Downloads an existing remote repo to your machine.
Use when: Joining a project or copying from GitHub.
git init
git init
Turns your current folder into a Git repository.
Use when: Starting a brand new local project.
3. THE DAILY CORE LOOP
git status (The Radar)
git status
Shows what is modified, staged, or untracked.
RUN THIS BEFORE AND AFTER EVERY OTHER COMMAND.
git add (Stage)
git add analysis.py
Adds a specific file to the staging area (The Bus).
git add .
Adds ALL changed files.
git commit (Save)
git commit -m "Fix data cleaning bug"
Takes a permanent snapshot of the staging area.
Rule: Messages must be imperative ("Fix", not "Fixed").
git push (Upload)
git push origin main
Uploads your local commits to GitHub (Remote).
4. HISTORY & INSPECTION
git log
git log --oneline --graph
Shows a clean, visual tree of your commit history.
Press 'q' to exit the log view.
git diff
git diff
Shows exact line-by-line changes in unstaged files.
git diff --staged
Shows changes that are staged (ready to commit).
5. BRANCHING (The Sandbox)
Never code on main. Create branches for experiments. If the experiment fails, delete the branch.
git branch
git branch
Lists all local branches. Active branch has a star (*).
git switch / checkout
git switch -c feature-model-v2
Creates a NEW branch and switches to it instantly.
git switch main
Switches back to the main branch safely.
Note: `checkout -b` does the exact same thing as `switch -c`.
git branch -d (Delete)
git branch -d feature-model-v2
Deletes the branch locally (must be on another branch).
6. SYNCING & MERGING
git pull (Download & Merge)
git pull origin main
Fetches changes from GitHub and merges them into your current branch.
Use when: Teammates updated the repo while you slept.
git merge
git merge feature-model-v2
Takes the changes from the feature branch and fuses them into your CURRENT branch.
7. MERGE CONFLICTS
Don't panic. Git pauses the merge because you and a teammate edited the exact same line.
<<<<<<< HEAD (Current Branch)
learning_rate = 0.01
=======
learning_rate = 0.005
>>>>>>> feature-tune (Incoming)
How to fix it:
1. Open the file in VS Code.
2. Delete the markers (<<<<, ====, >>>>).
3. Keep the code you want (or combine both).
4. git add [file]
5. git commit -m "Resolve conflict"
Abort Merge (Panic Button)
git merge --abort
Cancels the merge and puts everything back to normal.
8. EMERGENCY RECOVERY ZONE
"I MESSED UP, HOW DO I UNDO?"
1. "I staged a file by accident!"
git restore --staged file.py
Removes it from Staging, but keeps your code safe.
2. "I ruined my file. Reset it!"
git restore file.py
Deletes all uncommitted changes. Restores last save.
⚠️ DANGER: Your recent typing is lost forever.
3. "Typo in the last commit message!"
git commit --amend -m "New message"
Replaces the last commit message. Do NOT do this if already pushed!
4. "Undo my last commit, keep code"
git reset --soft HEAD~1
Deletes the commit, but puts all files back into Staging.
5. "NUKE IT. DELETE EVERYTHING."
git reset --hard HEAD~1
Deletes the commit AND the code changes forever.
9. GITHUB COLLABORATION
How Data Science teams work together without overwriting each other's code.
1. Ensure you are up to date
git checkout main
git pull
2. Create your isolated branch
git switch -c add-xgboost-model
3. Work, Add, and Commit
git add model.py
git commit -m "Add XGBoost"
4. Push branch to GitHub
git push -u origin add-xgboost-model
The `-u` links your local branch to the remote one.
5. Pull Request (PR)
Go to GitHub.com, click "Compare & Pull Request". Teammates review it, then it gets merged into main.
10. GIT MANTRAS
COMMIT EARLY.
COMMIT OFTEN.
NEVER PUSH DIRECTLY
TO MAIN.
GIT STATUS IS YOUR
BEST FRIEND.
RUN IT BEFORE YOU PANIC.
A COMMIT IS A SAVE POINT.
A BRANCH IS AN ALTERNATE UNIVERSE.