The site runs, the check passes, and the README’s first new to-do asks for an “Open now” badge that needs a date library. Installing a package turns the site into an npm project, and that move takes several commits. Somewhere in the middle, the home page will stop working for a while. Meanwhile, the site on main is the one the store uses, and some to-dos, like the two holiday hours fixes, can’t wait until the move is done.
In this lesson, you’ll give the move a branch of its own, so main keeps a working site the whole time. You’ll create the branch, switch between it and main, and delete a branch you don’t need. Start with the habit that a branch replaces.
Keep the Working Site Safe
Before Git, the usual way to try something risky was to copy the folder. You’d end up with generic-grocery, generic-grocery-vite, and, after a long evening, generic-grocery-vite-FINAL. You’ve seen where that leads. The previous developer’s deals-old.js and deals-FINAL2.js sat next to the real deals file, and the files didn’t say which one was live or why the others existed.
Git solves the same problem without a second folder. A branch is a name for a line of work in your repository. You make your commits on it, and the commits on other branches stay as they were. Your repository already has one branch, main, which you set as the default name in the last course. Every git status you’ve run says so on its first line.
To see every branch in the repository, run git branch in the terminal in your generic-grocery folder:
$ git branch
* main
The list has a single entry, and the asterisk marks the branch you’re on right now. Teams work on branches to keep their main line of work in a state they can ship. Branching is one of the everyday Git skills behind working on a team as a developer.
Create the vite Branch
The move to npm and Vite needs a branch that’s separate from main, and you can create it and switch to it with one command. Call the branch vite, after the tool you’ll use at the end of the move:
$ git switch -c vite
Switched to a new branch 'vite'
The git switch command moves you to another branch, and the -c option, short for create, makes that branch first. In older tutorials and AI suggestions, you’ll also see git checkout -b vite, which does the same thing. Ask Git for the list again:
$ git branch
main
* vite
Now there are two branches, and the asterisk has moved to vite. The bottom-left corner of VS Code’s status bar shows vite too, so you can always see where you are without typing a command.
Creating a branch can sound like a big step, so look at what changed in your project. The Explorer in VS Code shows the same files and folders as before. There’s no new folder next to generic-grocery or inside it, and git status agrees that your files didn’t change:
$ git status
On branch vite
nothing to commit, working tree clean
The only difference is the first line, which now says vite instead of main. Creating a branch doesn’t copy any files, because Git only writes down a new name for the commit you’re on. You can see that with git branch -v, where the -v option adds each branch’s latest commit:
$ git branch -v
main 09de33d Plan the move to npm and Vite
* vite 09de33d Plan the move to npm and Vite
Both names point at the same commit, the setup commit from the last lesson. As before, your hash will be different if you kept your own repository. The branches only start to differ once you commit on one of them, and from then on, each new commit you make on vite moves only vite forward, while main stays at the working site.
Two names for one commit can feel odd when the point of a branch is to keep work apart. If so, then ask the mentor about it. The button below opens a chat with the question filled in and the paragraph about the two names attached, and you can follow up with your own questions about what you saw in the terminal:
Whichever way the mentor puts it, vite differs from main only by the commits you’ll make on it, and you’ll make the first one in the next lesson.
Move Between Branches
With the move on its own branch, you can get back to main at any moment. When a holiday hours fix can’t wait, you’ll switch to main, commit the fix there, and switch back to the move. Try the trip now, while both branches are still the same:
$ git switch main
Switched to branch 'main'
$ git switch vite
Switched to branch 'vite'
Your files didn’t change on this trip, because both branches point at the same commit. Once vite has commits of its own, switching does more than move the asterisk. Git puts the files of that branch’s latest commit in your folder, so git switch main gives you back the site as it is on main, and git switch vite brings back the move in progress. It’s still one folder, and Git changes its contents for you.
That swap covers the work you’ve committed. Changes you haven’t committed yet aren’t on any branch, so before you switch, commit what belongs on vite and check that git status says nothing to commit, working tree clean. You’ll see why that matters soon.
Delete a Branch You Don’t Need
Branches are cheap to create, so you’ll also want a way to tidy up the ones you’re done with. Usually, that’s a branch whose work has been merged into another branch. You’ll merge vite into main at the end of this course. Git won’t delete the branch you’re on, so you switch to the branch that holds the merged work and delete the finished branch from there.
Practice on a throwaway branch called practice, and create it with git branch followed by the name. That form creates the branch without switching to it, so you stay on vite and can delete practice from there:
$ git branch practice
$ git branch
main
practice
* vite
The practice branch points at the same commit as the other two and has no work of its own. Delete it with the -d option, short for delete:
$ git branch -d practice
Deleted branch practice (was 09de33d).
Git confirms the deletion and names the commit the branch pointed at. The commit itself is still there, because main and vite point at it too. Deleting a branch removes only its name.
Git checks a branch’s commits only against the branch you’re on. If a branch has commits that your current branch doesn’t have, then git branch -d refuses with a message that the branch “is not fully merged”. When you see that, stop and leave the branch where it is, because its commits aren’t anywhere else yet.
You won’t see this refusal today, because practice had no commits of its own. If you meet it later, then come back to the button below and paste Git’s message into the chat, or press it now to hear what the message means:
For now, a refusal means the branch stays. You’ll learn to bring one branch’s work into another, and to see where that work ended up, when you merge branches later in this course.
Check Where You Are
You’re about to start the move, so make sure you’re on vite. Run the check script with node scripts/check.js, and look for the line about the branch:
Checking the Git repository in /Users/martin/projects/generic-grocery
✔ Git is installed (git version 2.43.0).
✔ The project folder is a Git repository.
✔ Your commits are labeled Martin Breuss <[email protected]>.
✔ .gitignore keeps notes.txt out of the repository.
✔ .gitignore keeps .DS_Store out of the repository.
✔ The history has 11 commit(s).
✔ npm is version 12 or later.
✔ You're on the vite branch.
✔ No tracked file holds a Generic Curbside secret key (curb_sec_...).
✔ Everything is committed.
All checks passed.
Your report shows your own name and folder. The line You’re on the vite branch. confirms that your next commits will go on vite. If it says main, then run git switch vite and check again. To see whether you can use branches on your own, answer these questions before you open the answers:
- You want to try a new layout for the departments page without touching
main. Which command creates a branch for it and moves you onto it? - You’re on
vite,git statussays the working tree is clean, and a typo on the live site needs fixing right away. Which command takes you to the branch for that fix? - You’ve merged a finished branch into
main. Which branch do you need to be on to delete it withgit branch -d, and what does Git do if your current branch doesn’t have the finished branch’s commits?
- Run
git switch -cwith a name for the branch, such asgit switch -c new-departments. It creates the branch at the commit you’re on and switches to it. - Run
git switch main. Because your work onviteis committed, Git puts the version of the files frommainin your folder, and you fix the typo on the live site’s line of work. - You need to be on
main, the branch you merged it into. Git won’t delete the branch you’re on. The-doption checks the finished branch’s commits against your current branch, and becausemainhas them, it removes only the name, as it did when you deletedpractice. If your current branch doesn’t have those commits, then-drefuses with “not fully merged”, and you leave the branch in place.
The vite branch is ready. It still points at the same commit as main, and the next commit you make on vite starts the move. In the next lesson, you’ll make that first commit by turning the site into an npm project with a package.json file.