Module 2 · Git & GitHub: From First Commit to Pull Request
Branches: Work Without Fear
Learn the idea that makes Git powerful, the branch. Create a separate line of work, switch between branches, merge finished work back, and tidy up, with a workflow every team uses.
About 30 minutes
The problem branches solve
You are building a website that people use. You want to add a new contact page. It will take three days, and half-finished code will break things in the meantime. Meanwhile, a visitor reports a typo that needs fixing today.
If everything is on one line of work, you are stuck. You cannot publish the typo fix without publishing your half-built contact page.
A branch solves this. It is a separate line of work that starts from where you are now. You do the risky, unfinished work on your own branch, while the main branch stays stable and ready to publish. When your branch is finished and tested, you merge it back in.
What a branch really is
Here is the secret, and it makes everything easier: a branch is just a label that points to a commit. Making a branch is instant and costs nothing, because Git does not copy your files. It only creates a new label.
Output
main: A --- B --- C
\
feature: D --- Emain still points at C. The feature branch started at C and has two more commits, D and E. Work on feature does not touch main.
The default branch is called main. (Older projects call it master.) A special pointer called HEAD says which branch you are on right now, and new commits are added to that branch.
The commands
| Command | What it does |
|---|---|
git branch | List branches. The current one has a * |
git branch name | Create a branch, but stay where you are |
git switch name | Move to another branch |
git switch -c name | Create a branch and switch to it (the one you will use most) |
git merge name | Bring that branch's work into the one you are on |
git branch -d name | Delete a finished branch |
(You will also see git checkout, the older command that does the same jobs. switch is clearer.)
Try it: your first branch
The project below is a small website with one commit. Follow along in the terminal:
git branchshows onlymain.git switch -c feature/contact-pagecreates the branch and moves you onto it.- Click New file (type
contact.html, press Add), write something in it, and save. git add contact.htmlandgit commit -m "Add contact page".git log --oneline: your new commit is on top, andHEAD -> feature/contact-pageshows where you are.git switch main. Look at the file list:contact.htmlhas disappeared! It is safe on your feature branch, but it is not part of main.git switch feature/contact-pageand the file comes back.
That moment is the whole idea: switching branches changes the files in your folder to match.
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Merging: bringing work back
When the contact page is done, you merge it into main. You switch to the branch that receives the work, and merge the other one in:
Shell (bash)
git switch main
git merge feature/contact-pageGit can merge in two ways:
- Fast-forward. If
mainhas not moved since you branched, Git simply moves themainlabel forward to your latest commit. No new commit is needed. You will seeFast-forward. - Merge commit. If both branches have new commits, Git combines the two and makes a special merge commit with two parents. Git handles it automatically when the changes are in different places. When the same lines changed on both sides, you get a conflict, the subject of the next lesson.
After merging, delete the finished branch. The commits are safe in main:
Shell (bash)
git branch -d feature/contact-pageNaming branches
Good branch names say what the work is. Teams often use a prefix:
| Name | For |
|---|---|
feature/contact-page | A new feature |
fix/login-typo | A bug fix |
docs/update-readme | Documentation |
experiment/dark-theme | Something you might throw away |
Use lowercase and hyphens, and no spaces.
A branching workflow that works
- Start from an up-to-date
main. git switch -c feature/something.- Make small commits, with good messages.
- When done and tested, merge into
main(on a team, through a pull request, which is lesson 8). - Delete the branch.
- Repeat.
This keeps main always working, which is a rule professional teams care about. main should always be in a state you could publish.
Switching with unfinished work
Git will not let you switch branches if your uncommitted changes would be overwritten:
Output
error: Your local changes to the following files would be overwritten by checkoutYou have three choices: commit your work, stash it (lesson 4), or discard it with git restore. Never ignore the message.
Try it
Do the full branch cycle: create a branch, commit on it, merge it back and delete it.
Task
10 minCreate a branch called feature/contact-page. On it, add a file contact.html and commit it. Then switch back to main, merge the branch in, and delete the branch.
Your repository is checked for
- You created the branch with git switch -c (or checkout -b)
- contact.html is part of main now (merged)
- You are back on main
- contact.html is tracked in the last commit on main
- The finished branch is deleted
- The history has at least two commits
- Everything is committed (clean)
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Now keep two pieces of work apart. Create two feature branches, each with its own commit, and finish on main without merging either.
Task
10 minTwo teammates will work in parallel. On a branch dark-mode, add and commit a file dark.css. Starting again from main, on a branch search, add and commit search.js. When you finish, switch back to main. Neither file should be in main's folder.
Your repository is checked for
- A branch called dark-mode exists
- A branch called search exists
- You finished on main
- dark.css is NOT in main's folder (it lives only on dark-mode)
- search.js is NOT in main's folder (it lives only on search)
- You made each branch with git switch -c or git checkout -b
- Everything is committed (clean)
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Practice
Which single command creates a new branch called fix/typo and switches to it? Type the full command.
Earn your badge
Take the module check
Questions on this module. Pass and you earn the Git Branching badge, free.
Create a free account to earn the badge