Nishaglobal Education logo

Branches: working on ideas safely

Use Git branches to work on new features without touching the working version: create and switch branches with git switch, see where you are, commit on a branch and delete branches you no longer need.

Lesson 6 of 1130 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Explain what a branch is and why teams use them
  • Create, list and switch branches with git branch and git switch
  • Commit on a branch and see that main is unchanged
Outline
  1. What you'll learn and why it matters
  2. Which branch am I on?
  3. Create and switch to a branch
  4. Switch between branches
  5. See every branch
  6. Delete a branch
  7. Practice
  8. Common mistakes
  9. Recap
  10. Quiz
  11. Mark lesson complete

What you'll learn and why it matters

Imagine your website works, and you want to try a new contact page. If you edit main directly and get halfway, the site is half-broken. A branch is a separate line of work: you build the contact page on its own branch, and main stays exactly as it was until the new work is ready. Professional teams do almost all their work on branches.

Which branch am I on?

Terminal
git branch

Output

* main

There is one branch, main, and the * marks the one you are on. git status also tells you on its first line.

Create and switch to a branch

git switch -c creates a new branch and moves onto it in one step. Name branches after the work, with hyphens instead of spaces:

Terminal
git switch -c contact-page
git branch

Output

Switched to a new branch 'contact-page'
* contact-page
  main

Now commit on the new branch as usual:

Terminal
printf '<h1>Contact</h1>\n<p>Email: asha@example.com</p>\n' > contact.html
git add contact.html
git commit -m "Add contact page"
git log --oneline

Output

[contact-page 4970bdd] Add contact page
 1 file changed, 2 insertions(+)
 create mode 100644 contact.html
4970bdd (HEAD -> contact-page) Add contact page
1ce7ed1 (main) Add intro sentence
ec605b9 Add stylesheet
6103896 Add home page
6103ec601ce74970maincontact-pagebranch starts at 1ce7The contact page commit exists only on contact-page.
The main branch has three commits. A contact-page branch starts from the latest one and has one extra commit, Add contact page. The main label still points at Add intro sentence, so main does not include the contact page.

Switch between branches

Switching branches changes the files in your folder to match that branch. Watch contact.html appear and disappear:

Terminal
ls
git switch main
ls
git switch contact-page
ls

Output

contact.html
index.html
styles.css
Switched to branch 'main'
index.html
styles.css
Switched to branch 'contact-page'
contact.html
index.html
styles.css

The file is not lost when you switch to main: it is safely stored in the contact-page branch's commit. This is why commits are so useful.

CommandDoes
git branchList branches; * marks the current one
git switch -c <name>Create a branch from here and switch to it
git switch <name>Switch to an existing branch
git branch -d <name>Delete a branch whose work has been merged
git log --oneline --all --graphShow every branch's commits as a picture

See every branch

Terminal
git log --oneline --all --graph

Output

* 4970bdd (contact-page) Add contact page
* 1ce7ed1 (HEAD -> main) Add intro sentence
* ec605b9 Add stylesheet
* 6103896 Add home page

Each * is a commit. The contact-page branch is one commit ahead of main. In lesson 7 you will bring that commit into main by merging.

Delete a branch

When work on a branch is finished and merged (lesson 7), delete the branch to keep things tidy. Git protects you from deleting work that is not merged yet:

Terminal
git branch -d contact-page

Output

error: the branch 'contact-page' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D contact-page'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

Git refused, because main does not contain the contact page yet. (-D with a capital D forces the deletion and throws the work away, so only use it when you really mean it.)

Practice

Guided exercise

Try a new colour on a branch

Create a branch called dark-theme, change styles.css on it and commit. Then switch back to main and show that styles.css still has the old colour.

  1. git switch -c dark-theme
  2. Change the file and commit it with a clear message.
  3. git switch main and cat styles.css.
Show the solution
Solution
git switch -c dark-theme
printf 'body { background: #111; color: #eee; }\n' > styles.css
git commit -q -am "Try a dark theme"
git switch main
cat styles.css

Output

Switched to a new branch 'dark-theme'
Switched to branch 'main'
h1 { color: navy; }

How to get there: git commit -am stages every change to files Git already tracks and commits them in one go (it does not add new files). After switching back, main still has the original stylesheet; the dark theme lives only on its branch.

Your turn

Branch overview

In the project with the contact-page branch, list the branches, then show the graph of all branches.

Show the solution
Solution
git branch
git log --oneline --all --graph

Output

  contact-page
* main
* 4970bdd (contact-page) Add contact page
* 1ce7ed1 (HEAD -> main) Add intro sentence
* ec605b9 Add stylesheet
* 6103896 Add home page

How to get there: git branch shows two branches, with * on main because that is where you are. The graph shows contact-page one commit ahead of main.

Common mistakes

Working on main by accident

You see: Half-finished work ends up on the main branch

Fix: Check the first line of git status before you start, and create a branch for each piece of work.

Spaces in branch names

You see: fatal: 'contact page' is not a valid branch name

Fix: Use hyphens: contact-page.

Switching with uncommitted changes

You see: error: Your local changes … would be overwritten

Fix: Commit or restore your changes first.

Thinking files are lost after switching

You see: A file you made disappears

Fix: It is safe in the other branch; switch back to see it.

Recap

  • A branch is a separate line of commits; main stays safe while you work.
  • git switch -c name creates and switches; git switch name switches.
  • Switching changes your files to match the branch; commit before switching.
  • git branch -d deletes merged branches; git log --oneline --all --graph shows them all.

Key terms

Branch
A movable label pointing at a line of commits, used to work on something separately.
main
The usual name of a repository's main branch, holding the working version.
git switch
The command for moving between branches (and creating them with -c).

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which command creates a branch called fix-menu and switches to it?

  2. 2. In git branch output, what does * mean?

  3. 3. You commit contact.html on a branch, then switch to main. Where is contact.html?

  4. 4. Why does git branch -d refuse to delete an unmerged branch?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.