# Chapter 16: Version Control ## The Problem Your marketing configurations are files now—YAML for segments, journeys, and workflows. Files that change over time. Files that multiple people might edit. How do you track what changed? How do you undo mistakes? How do you know who changed what and why? ## The Key Idea Core concept Git tracks every change to every file, with history, authorship, and the ability to undo anything. Git is how developers have managed code for decades. Your marketing configurations are code now. Git works for you too. ## What Git Does | Scenario | Without Git | With Git | | --- | --- | --- | | Something broke | "What changed?" | `git diff` shows exactly what | | Need to undo | Hope you have a backup | `git revert` undoes it | | Who changed this? | Ask around | `git blame` shows authorship | | Multiple editors | Overwrite each other | Changes merge safely | ## How Git Works When you run `git init`, Git creates a hidden `.git` folder: ``` my-marketing-configs/ ├── .git/ ← Git's database (hidden) │ ├── objects/ ← All file versions stored here │ └── ... ├── segments/ │ └── high-value.yml └── workflows/ └── daily-refresh.dig ``` Everything Git knows lives in that `.git` folder—the complete history of every file, every change, every author. Your working files are just the current version. **Key insight:** Git is local. The history is on your computer. No internet required to commit, view history, or undo changes. To share with others, you `git push` to a remote server like GitHub. That's [Chapter 17](/treasure-code/book/17-github-workflows). ## Core Concepts ```mermaid flowchart LR W[Working Files] -->|git add| S[Staging Area] S -->|git commit| H[History] H -->|git checkout| W ``` **Working files:** The files you see and edit. **Staging area:** Changes you've marked to include in the next commit. **History:** The chain of commits stored in `.git`—your time machine. **Commit:** A snapshot of staged changes, with a message and timestamp. Each commit has a unique ID like `a3f2c1d`. ## Getting Started AI can help you set up Git: ``` > "Initialize a Git repository for my marketing configurations" ``` ```bash git init git add . git commit -m "Initial commit: segments and workflows" ``` Your files are now tracked. ## Saving Changes (Commits) After making changes, save them: ``` > "Commit my changes with message 'Updated high-value threshold to $1000'" ``` ```bash git add high-value-customers.yaml git commit -m "Updated high-value threshold to $1000" ``` This creates a snapshot you can always return to. ## Viewing History See what changed over time: ``` > "Show me the commit history" ``` ``` commit a3f2c1d (2 hours ago) Author: Sarah Message: Updated high-value threshold to $1000 commit 8b4e2f9 (yesterday) Author: Mike Message: Added purchase behavior to segment commit 1c7d3e8 (last week) Author: Sarah Message: Initial segment creation ``` Every change is recorded, with who made it and when. ## Seeing What Changed View differences between versions: ``` > "Show me what changed in the last commit" ``` ```diff rules: - attribute: lifetime_value operator: Greater - value: 500 + value: 1000 ``` Lines starting with `-` were removed. Lines with `+` were added. ## Undoing Changes **Undo uncommitted changes:** ``` > "Discard my changes to high-value-customers.yaml" ``` ```bash git checkout high-value-customers.yaml ``` **Undo a commit:** ``` > "Revert the last commit" ``` ```bash git revert HEAD ``` This creates a new commit that undoes the previous one. **Go back in time:** ``` > "Show me what this file looked like last week" ``` ```bash git show HEAD~5:high-value-customers.yaml ``` ## Who Changed What (Blame) Find out who wrote each line: ``` > "Who last modified the threshold value?" ``` ```bash git blame high-value-customers.yaml ``` ``` a3f2c1d (Sarah 2 hours ago) value: 1000 ``` Useful when you need to ask someone about a change. ## Branches: Safe Experimentation Create a branch to try something without affecting the main files: ``` > "Create a branch called 'experiment-new-rules'" ``` ```bash git checkout -b experiment-new-rules ``` Make changes, test them. If they work, merge back: ``` > "Merge my experiment into main" ``` If they don't work, delete the branch—main is untouched. ## Mental Model: Time Machine Git is a time machine for your files: ``` [commit 1] → [commit 2] → [commit 3] → [HEAD] "Initial" "Add rule" "Fix typo" (you are here) ``` - **HEAD** is where you are now - Every commit knows its parent (the previous commit) - You can jump to any point in history - Nothing is ever truly deleted—old versions live in `.git` The `.git` folder is your time machine. Delete it, and you lose all history. Keep it, and you can always go back. ## Common Git Commands | What You Want | Command | What AI Says | | --- | --- | --- | | Save changes | `git add . && git commit -m "message"` | "Commit my changes" | | See history | `git log --oneline` | "Show commit history" | | See differences | `git diff` | "What's changed?" | | Undo changes | `git checkout ` | "Discard changes to X" | | Create branch | `git checkout -b ` | "Create a branch for X" | | Switch branch | `git checkout ` | "Switch to branch X" | AI translates your intent into these commands. ## Working with AI AI and Git work well together: ``` > "Create a segment for churned customers" ``` AI creates the file. Then: ``` > "Commit this with message 'Add churned customers segment'" ``` The AI-generated work is now tracked in history. ## Best Practices **Commit often with clear messages:** - Good: "Increased LTV threshold to $1000 for Q1 campaign" - Bad: "Updated stuff" **One change per commit:** - Good: Two commits, one for each segment - Bad: One commit changing five unrelated files **Review before committing:** ``` > "Show me what I'm about to commit" ``` ## Pitfalls **"I committed the wrong thing."** Undo it immediately: ``` > "Undo my last commit but keep the changes" ``` **"I'm on the wrong branch."** Switch branches: ``` > "Switch to main branch" ``` **"I have merge conflicts."** This happens when two people edit the same line. AI can help: ``` > "Help me resolve these merge conflicts" ``` ## What You've Learned - Git tracks every change with full history - Commits are snapshots with messages - You can view, compare, and undo any change - Branches let you experiment safely - AI translates your intent into Git commands ## Next Step Git tracks your changes locally. [Chapter 17](/treasure-code/book/17-github-workflows) shows you how to collaborate with your team—sharing changes, reviewing each other's work, and approving before deploying. *Your changes are tracked. Next, you'll learn to collaborate.*