Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Picture How Branches Move

In this video, you drew the store’s history as commits and watched what the branch names do when you commit and switch. Here are the key points:

  • A branch is a name that points to one commit. Creating vite with git switch -c vite didn’t copy any files. It put a second name on the commit that planned the move, where main already pointed.

  • The name you’re on moves forward with each commit. Your two commits on vite, one that added package.json and one that added the dayjs entry and the lockfile, each moved vite one commit further. main stayed on the commit that planned the move, which is how it keeps the working site.

  • Switching changes which name you’re on, and your files follow. git branch marks the branch you’re on with an asterisk. After git switch main, package.json and package-lock.json are gone from the folder, because the commit main points to doesn’t have them. node_modules/ stays, because Git ignores it and never tracked it. git switch vite brings both files back.

  • Git doesn’t record where a branch started. vite is a name on the dayjs commit, and that commit points back through its parents. main is a branch like any other, the first name your repository had.

  • Deleting a branch deletes the name, not the commits. When you deleted practice with git branch -d practice, the commit it pointed to stayed where it was.

These are the commands from the video, with the folder listing on each branch:

Language: Shell
$ git branch
  main
* vite
$ ls -C
README.md  deals.html        index.html    package-lock.json  pickup.html  src
css        departments.html  node_modules  package.json       scripts
$ git switch main
Switched to branch 'main'
$ ls -C
README.md  deals.html        index.html    pickup.html  src
css        departments.html  node_modules  scripts
$ git switch vite
Switched to branch 'vite'

With that picture in mind, you’re ready for the badge’s code on vite. In the next lesson, you’ll add it, and the home page will stop working in the browser, which is the problem Vite’s dev server solves.

00:00 You’ve made two commits on vite now, one for package.json and one for dayjs. Meanwhile, main still holds the site that works today. But you never saw a second copy of the project, not even a new folder.

00:14 So where does a branch live, and how can main stay put while vite moves on?

00:20 Ask Git with git branch. It lists two names, main and vite, and the asterisk marks the one you’re on. Here’s the history you’ve read with git log, drawn as dots.

00:34 Each dot is a commit, with its message, and each one points back to its parent. Before you made the branch, main was a name on the newest commit, the one that planned the move.

00:47 When you ran git switch with the -c option, Git didn’t copy any files. It wrote a second name, vite, on that same commit, and made it the branch you’re on.

00:58 That’s all a branch is, a name that points to one commit. Picture it as a sticky note on a dot. But a sticky note stays where you put it, and a branch doesn’t.

01:10 Then you committed package.json. Git made a new commit, and its parent is the commit you were on. Git also moved the name you’re on, vite, to the new commit.

01:21 Main stayed where it was. Next, you committed the dayjs entry in package.json and the lockfile, still on vite. Again, only vite moved, one dot further. Main is still on the commit that planned the move.

01:38 That’s how main keeps the working site. You haven’t committed on main since you made vite.

01:45 Follow along in your terminal. Run git switch main to get back to the working site. Git confirms the switch.

01:53 The asterisk moves to main, so main is the branch you’re on. Neither name moved. Git also swaps the files in your folder for the snapshot in main’s commit.

02:04 Your turn: pause the video and predict which of package.json, the lockfile and node_modules are still in your folder.

02:17 It’s package.json that’s gone, and so is the lockfile.

02:22 List the folder with ls, and neither file is there. The commit that planned the move doesn’t have them. The node_modules folder stays, because Git ignores it and never tracked it.

02:34 Switch back to vite, and both files return from vite’s commit. Switching changes which name you’re on, and your files follow that name’s commit.

02:44 Look at the picture now. Git doesn’t record which branch vite came from. It only has the names, and the arrows between commits. Vite is a name on the dayjs commit, and that commit points back through its parents.

02:58 Main is a branch like any other, the first name your repository had.

03:03 This also explains the practice branch you deleted earlier. Git branch with the -d option removed the name. The commit it pointed to stayed where it was.

03:12 Deleting a branch deletes the name, not the commits.

03:17 So a branch is a name that points to one commit. When you commit, the name you’re on moves forward. When you switch, you change which name you’re on, and your files follow.

03:29 With that picture in mind, you’re ready for the badge’s code on vite. In the next lesson, you’ll add it, and the home page will stop working in the browser.

03:37 That’s the problem Vite’s dev server solves.

Become a Member to join the conversation.