AI Hints
Home›Git & GitHub Tutorial›Working with Branches
Git & GitHub · Lesson 42 of 144

Working on a Branch

A feature, start to finish.

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

Let's do a real feature on a branch, start to finish. We'll add a contact page, on its own branch, while main stays untouched.

Working on a Branch
From the video: Working on a Branch
  • A real feature
  • On its own branch
  • main stays untouched

Building the feature

Create and switch to a branch called contact. Write the contact page, and commit it. Then add a link to it from the home page, and commit again. Two commits on the contact branch.

git
git switch -c contact
Switched to a new branch 'contact'
echo '<h1>Contact me</h1>' > contact.html
git add contact.html
git commit -m "Add contact page"
[contact 2ad55a2] Add contact page
 1 file changed, 1 insertion(+)
 create mode 100644 contact.html
echo '<a href="contact.html">Contact</a>' >> index.html
git commit -am "Link to contact page"
[contact 90dc6d2] Link to contact page
 1 file changed, 1 insertion(+)

Comparing branches

Now let's compare. The log on contact shows our two new commits on top. The log of main shows main hasn't changed at all. Its latest commit is still link to about page.

git
git log --oneline -n 3
90dc6d2 (HEAD -> contact) Link to contact page
2ad55a2 Add contact page
f63a17a (main) Link to about page
git log --oneline -n 3 main
f63a17a (main) Link to about page
ef9c947 Add basic styles
6784119 Add about page

Summary

You built a whole feature on a branch, and main never changed. Next, keeping your list of branches tidy.

Key takeaways
  • Feature on its own branch
  • Several commits
  • main unchanged
Next up: Listing and Deleting Branches — git branch -d