GITHUB / LESSON 06
Branches.
Working on features in isolation

Loading the lesson…
Loading activities…
GITHUB / LESSON 06
Working on features in isolation

Loading the lesson…
Loading activities…
So far you've been working on one single version of your project. Which is fine... right up until you want to try something new and you're nervous about wrecking the code that already works. That's EXACTLY what branches are for.
A branch is basically a copy of your project where you can mess around freely. Your original stays safe the whole time you're trying new things.
Picture yourself writing an essay.
Every repository has a default branch, usually called main (older repositories might call it master). This is treated as the "official" version of your project.
The rule. Keep main working at ALL times. Never push broken code directly to main. My dad's rule was "do it right or don't do it at all," and main is where that rule lives.
Alright, let's make a new branch and try something out.
You'll see * main. That lil star shows which branch you're on.
This creates a new branch called add-feature and switches you over to it in one move.
Branch naming tips.
fix-login-bug, add-contact-page or update-styles (future you will be grateful)Now you'll see this.
The star moved to add-feature. You're in.
From here on, any changes you make land on the add-feature branch. Your main branch stays exactly how you left it.
Your change is saved on the add-feature branch. Over on main, that file doesn't exist at all.
You can hop back to main whenever you want.
Now look at your files. feature.txt is GONE! Don't panic... it still exists on the add-feature branch.
Switch back.
Aaand feature.txt is back. Each branch keeps its own version of your project.
Right now your new branch only exists on your computer. To save it to GitHub, run this.
The -u origin add-feature part tells Git to create this branch on GitHub and hook it up to your local branch. After that first push, plain old git push does the job.
Happy with your changes? Then it's time to merge them back into main.
Git takes the changes from add-feature and combines them into main.
Now main has all the changes you made on add-feature. Donezo!
Once it's merged, you might not need that branch hanging around anymore.
That deletes the branch locally, on your computer. To delete it on GitHub too, run this.
Here's how branches usually get used, start to finish.
main with working codegit checkout -b new-featuregit push -u origin new-featuremainThe top row is main and the bottom row is the new-feature branch. It splits off, picks up a few commits, and then merges back into main.
Screenshot this one. You'll be back.
| Command | What It Does |
|---|---|
git branch | List all branches |
git checkout -b name | Create and switch to new branch |
git checkout name | Switch to existing branch |
git merge name | Merge branch into current branch |
git branch -d name | Delete a branch |
git push -u origin name | Push new branch to GitHub |
mainmain branch should ALWAYS have working codegit checkout -b branch-namegit checkout branch-nameMerging directly works fine, but on a team you usually want somebody to look over your code first. In the next lesson you'll learn about Pull Requests, which is how you propose changes and get feedback before anything gets merged...