Imagine you're building a feature but your code breaks halfway through. If everything is on main, your broken code is now live. Branches let you work in a safe sandbox โ completely isolated from the rest of the project until you're ready.
Your project is a book. main is the published version. A branch is like a personal notepad where you draft a new chapter. Once the chapter is good, you copy it into the book (merge). If you hate it, just throw the notepad away โ the book is untouched.
main (or master) as the stable production branch. New work always happens on feature branches, never directly on main.
| Command | What it does |
|---|---|
| git branch | List all branches. The one with * is your current branch. |
| git branch <name> | Create a new branch. Doesn't switch to it yet. |
| git checkout <name> | Switch to an existing branch. |
| git checkout -b <name> shortcut | Create AND switch in one command. You'll use this 90% of the time. |
| git merge <name> | Merge the named branch INTO your current branch. Always switch to the target branch first. |
| git branch -d <name> | Delete a branch after merging. Use -D to force delete. |
REAL WORKFLOW EXAMPLE
checkout main, then merge feature.
Try the commands yourself! This simulator mimics a real Git repo. Type commands below and see what happens.
Git simulator initialized. You have a repo with 2 commits on main.
Try: git branch, git checkout -b dev, git merge dev
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
feature/login AND immediately switch to it. Which command do you use?feature/login and you want to merge your work into main. What's the correct order of commands?feature/login into main, you want to delete the feature branch. Which command is safe to use?git branch and see:* main
dev
feature/login-b flag stand for in git checkout -b?Now use what you learned on the real StatLab project you built today. Open your terminal, navigate to your project folder, and follow these steps:
git branch โ you should only see main and dev. The feature branch should be gone. Run git log --oneline to see a clean history.