AI Hints
Git & GitHub · Lesson 41 of 144

HEAD and Branches

Where you are.

⏱ 2 min read▶ Video at 44:22🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Remember head? It means you are here. When you switch branches, head moves to the branch you switched to. And when you commit, only that branch moves forward.

HEAD and Branches
From the video: HEAD and Branches
  • HEAD means you are here
  • Switching moves HEAD
  • Committing moves your branch

Following HEAD

Here, main and dark both point to the same commit, and head is on main. We switch to dark, and head moves over. Now we commit. Only dark moves forward, carrying head with it. Main stays exactly where it was.

Proving it

Let's prove it in the terminal. On branch dark, we write a dark style file, add it, and commit it. Now switch back to main, and list the files. The dark style file isn't here. It only exists on the dark branch. Switch to dark again, and it's back.

git
git switch -c dark
Switched to a new branch 'dark'
echo "body { background: black; }" > dark.css
git add dark.css
git commit -m "Add dark theme"
[dark 8d00b30] Add dark theme
 1 file changed, 1 insertion(+)
 create mode 100644 dark.css
git switch main
Switched to branch 'main'
ls
about.html
index.html
style.css
git switch dark
Switched to branch 'dark'
ls
about.html
dark.css
index.html
style.css

Summary

Head follows you as you switch, and only the branch you're on moves when you commit. Each branch has its own version of the files. Next, real work on a branch.

Key takeaways
  • HEAD moves when you switch
  • Only your branch moves on commit
  • Files change per branch
Next up: Working on a Branch — a feature, start to finish