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.
🎯 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
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?
git branchOutput
* 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:
git switch -c contact-page
git branchOutput
Switched to a new branch 'contact-page' * contact-page main
Now commit on the new branch as usual:
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 --onelineOutput
[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
Switch between branches
Switching branches changes the files in your folder to match that branch. Watch contact.html appear and disappear:
ls
git switch main
ls
git switch contact-page
lsOutput
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.
| Command | Does |
|---|---|
git branch | List 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 --graph | Show every branch's commits as a picture |
See every branch
git log --oneline --all --graphOutput
* 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:
git branch -d contact-pageOutput
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.
git switch -c dark-theme- Change the file and commit it with a clear message.
git switch mainandcat styles.css.
Show the 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.cssOutput
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
git branch
git log --oneline --all --graphOutput
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 namecreates and switches;git switch nameswitches.- Switching changes your files to match the branch; commit before switching.
git branch -ddeletes merged branches;git log --oneline --all --graphshows 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. Which command creates a branch called fix-menu and switches to it?
2. In git branch output, what does * mean?
3. You commit contact.html on a branch, then switch to main. Where is contact.html?
4. Why does git branch -d refuse to delete an unmerged branch?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.