Branches
Imagine you want to try a completely new look for your website. Maybe dark mode, maybe a new layout. But your current version works, and you don't want to break it while you experiment. That's exactly what branches are for.
What a branch is
A branch is a separate line of work inside the same repository. When you create a branch, you get a copy of your project's history to work on. You can change as much as you like there, and the original stays untouched.
Your repository already has one branch, called main. Think of it as the official version, the one that works. Other branches are where you try things out. When an experiment works, you bring it back into main. When it doesn't, you just throw the branch away.
It's like writing a new chapter in a draft document. If it's good, you copy it into the real book. If not, the real book was never affected.
Creating and switching branches
To see your branches and which one you're on, run:
git branch
The current branch has a star next to it. Right now that's main.
To create a new branch and switch to it in one go:
git switch -c dark-mode
-c means "create". The branch name should describe what you're working on, using lowercase and dashes, just like file names.
To switch back to an existing branch, leave out the -c:
git switch main
Here's the magic part: when you switch branches, the files in your folder change to match that branch. Make some changes on dark-mode, commit them and switch to main. Your folder goes back to how main looks. Switch back to dark-mode and your changes are there again. Try it with Go Live open and watch the page change.
You'll also see the current branch name in the bottom left corner of VS Code. You can click it to switch branches there too.
You might see git checkout -b dark-mode in older guides. It does the same thing as git switch -c dark-mode. switch is just the newer, clearer command.
Which command creates a new branch called new-footer and switches to it?
Select one answer
Merging a branch
When your experiment works and you want it in the official version, you merge it. First switch to the branch you want to bring the changes into, then merge the other branch:
git switch main git merge dark-mode
All the commits from dark-mode are now part of main. Since you don't need the experiment branch anymore, you can delete it:
git branch -d dark-mode
You want the changes from your dark-mode branch in main. Which branch should you be on when you run git merge dark-mode?
Select one answer
Before you switch, commit
If you have changes that aren't committed and try to switch branches, Git sometimes refuses and shows a message about your changes being overwritten. That's Git protecting your work.
The simplest habit is: commit before you switch. Run git status, and if there's anything changed, commit it first.
Merge conflicts, briefly
Sometimes the same line was changed in both branches, and Git can't know which version you want. That's called a merge conflict.
It sounds scary, but it's just Git asking you to decide. VS Code highlights the conflicting lines and gives you buttons: Accept Current Change, Accept Incoming Change or Accept Both Changes. Pick the right one, save, then commit:
git add . git commit -m "Merge dark-mode into main"
If you only work on one branch at a time and merge regularly, conflicts are rare. When they do happen, take a breath. Nothing is lost.
What is a merge conflict?
Select one answer
Try an experiment on a branch:
- Commit any open changes on
main - Create a branch with
git switch -c new-colors - Change your color variables to something completely different and commit
- Switch back with
git switch mainand watch the page go back to the old colors - Decide which version you like better. If you like the new one, merge it with
git merge new-colors. If not, delete the branch withgit branch -D new-colors(a capital D deletes a branch that hasn't been merged)
Run git log --oneline at the end to see your history.