Git Branches

Work on features in isolation.

✍️ 2 exercises in this lesson

A branch is an independent line of development. The default branch is usually main.

  • git branch: list branches.
  • git switch -c feature-login: create a branch and switch to it.
  • git switch main: switch back.
  • git branch -d feature-login: delete a merged branch.

Commits made on a feature branch do not affect main until you merge them.

Key Takeaways: Git Branches

Mastering Git Branches helps you write clean and reliable Git code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Git Branches Code Example

Git
git switch -c feature-contact
echo "<h1>Contact</h1>" > contact.html
git add contact.html
git commit -m "Add contact page"
git branch
Output
Switched to a new branch 'feature-contact'
[feature-contact 1a9e3c4] Add contact page
  1 file changed, 1 insertion(+)
* feature-contact
  main
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Git Branches Practice Exercises

Check what you learned in “Git Branches”.

/ 2 solved
Fill in the blank

Create a new branch and switch to it.

git switch  feature-login
✓ Correct! Not quite. Try again.
Multiple choice

What is the default branch usually called today?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services