Unit 3 · Branching and Merging
Work in parallel, then bring it together
Branches let the team try ideas without breaking main. Learn to merge their work back, and to stay calm when two edits collide.
Why branches?
The Amroli Canteen app works, and Hetvi has shown it to the canteen manager. Now Krish wants to try a new menu design, and Dhruvi wants to add online payment. Both changes will take days, and both might break things.
If they edit main directly, the working app is broken until they finish. If they don't, they can't save their progress. Branches solve this.
Parallel universes
A branch is a parallel universe for your project. Krish can try his new design in his own universe. If it works, he brings it back to the main universe (a merge). If it fails, he deletes that universe, and main never even knew.
With branches:
mainalways stays working, ready to demo.- Each feature gets its own branch:
feature-menu,feature-payment. - Several people work in parallel without disturbing each other.
- Finished work is merged back into
main.
What a branch really is
A branch is just a movable label on a commit. HEAD shows which branch you are on. Committing moves that label forward.
A branch is not a copy of your files. It is a tiny label that points to one commit, which is why creating a branch is instant.
- When you commit, the current branch's label moves forward to the new commit.
- HEAD is a special label that says which branch you are on. Git Bash shows it in the prompt:
~/amroli-canteen (feature-menu).
Creating and listing branches
$ git branch* main$ git branch feature-menu$ git branch feature-menu* main
git branchlists branches. The*marks the one you are on.git branch <name>creates a branch at your current commit, but it does not switch to it.
Naming branches
Use short, descriptive, lowercase names with hyphens: feature-menu, fix-cart-total, docs-readme. No spaces.
Switching branches
$ git switch feature-menuSwitched to branch 'feature-menu'$ echo "Vada Pav 20" >> menu.txt$ git commit -am "Add vada pav to menu"[feature-menu b97cb83] Add vada pav to menu 1 file changed, 1 insertion(+)$ git log --oneline --graph --all* b97cb83 (HEAD -> feature-menu) Add vada pav to menu* 8391a89 (main) Add opening time* 3c16dcc Add menu
Now switch back to main and look at the menu:
$ git switch mainSwitched to branch 'main'$ cat menu.txtSamosa 15
The Vada Pav line is gone. That's not lost work: it is safely on feature-menu. Switching branches changes the files in your folder to match that branch.
Create and switch in one step:
| New way (Git 2.23+) | Classic way | Does |
|---|---|---|
git switch <branch> | git checkout <branch> | move to an existing branch |
git switch -c <new> | git checkout -b <new> | create a branch and move to it |
Both work. checkout is older and does many jobs (it also restores files), so newer Git added the clearer switch. You will see checkout in many tutorials, so know both.
Commit before you switch
If you have uncommitted changes to a file that is different on the other branch, Git refuses to switch: "Your local changes to the following files would be overwritten by checkout". Commit your work (or stash it, Unit 4) first.
Merging: fast-forward
Krish is happy with the new menu. To bring feature-menu into main, switch to the branch that receives the changes, then merge:
$ git switch main$ git merge feature-menuUpdating 8391a89..b97cb83Fast-forward menu.txt | 1 + 1 file changed, 1 insertion(+)
Fast-forward means main had no new commits of its own, so Git simply moved the main label forward to Krish's commit. No new commit was needed.
Three-way merges
Fast-forward
main had no new commits, so Git just moves the label forward. No merge commit.
Three-way merge
Both branches moved on, so Git joins them with a new merge commit that has two parents.
Meanwhile Krish built the cart on feature-cart, and someone also added tea to main. Now both branches have new commits:
$ git merge feature-cartMerge made by the 'ort' strategy. cart.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 cart.js$ git log --oneline --graph* de0d069 (HEAD -> main) Merge branch 'feature-cart'|\* | 3776e7d Add tea| * 7c7c199 (feature-cart) Add cart* | b97cb83 (feature-menu) Add vada pav to menu|/* 8391a89 Add opening time* 3c16dcc Add menu
Git found the common ancestor (8391a89), combined both sets of changes, and created a merge commit with two parents. Read the graph from the top: the |\ is where the history splits; the |/ is where it started.
Opens an editor?
On a real computer, git merge may open your editor with the message Merge branch 'feature-cart'. Just save and close it to accept.
Merge conflicts
Sometimes Git cannot decide alone. Dhruvi changed the opening time to 8 AM on her branch; on main, Krish changed the same line to 10 AM.
$ git merge change-timingAuto-merging timing.txtCONFLICT (content): Merge conflict in timing.txtAutomatic merge failed; fix conflicts and then commit the result.$ git statusOn branch mainYou have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: timing.txt
A conflict happens when two branches change the same lines of the same file. Git stops and asks a human to decide. That isn't an error: it is Git refusing to guess.
Git writes both versions into the file, between markers:
<<<<<<< HEAD Open: 10 AM ======= Open: 8 AM >>>>>>> change-timing
<<<<<<< HEADstarts your version (the branch you are on)=======divides the two versions>>>>>>> change-timingends their version (the branch being merged)- To resolve: keep the right text, delete all three marker lines, then
git addandgit commit
Resolving a conflict, step by step:
- Open the file (
edit timing.txtin the Playground, or your editor). - Decide the correct content. Keep one side, the other, or a mix.
- Delete all three marker lines.
- Mark it resolved:
git add timing.txt. - Finish the merge:
git commit.
$ echo "Open: 8 AM" > timing.txt$ git add timing.txt$ git statusOn branch mainAll conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: timing.txt$ git commit --no-edit[main e41ab0c] Merge branch 'change-timing'
The escape hatch
Changed your mind halfway? git merge --abort puts everything back exactly as it was before the merge.
Practise resolving conflicts
Tap lines to delete them until each file matches what the team decided, with no markers left.
Loading the conflict game…
Deleting branches
After a branch is merged, its label is no longer needed. Deleting it keeps git branch tidy (the commits stay in main's history):
$ git branch -d feature-menuDeleted branch feature-menu (was b97cb83).
Git protects you from deleting work that was never merged:
$ git branch -d experimenterror: the branch 'experiment' is not fully mergedhint: If you are sure you want to delete it, run 'git branch -D experiment'hint: Disable this message with "git config set advice.forceDeleteBranch false"$ git branch -D experimentDeleted branch experiment (was 84079e6).
| Command | Deletes |
|---|---|
git branch -d <name> | a merged branch (safe) |
git branch -D <name> | any branch, even unmerged (its unique commits are abandoned) |
git push origin --delete <name> | the branch on GitHub |
You can't delete the branch you are standing on; switch away first.
Branches on GitHub
Branches can be pushed like main, so teammates can see or review them:
$ git push -u origin feature-paymentTo https://github.com/krish-patel/amroli-canteen.git * [new branch] feature-payment -> feature-paymentbranch 'feature-payment' set up to track 'origin/feature-payment'.$ git branch -a change-timing feature-cart* feature-payment main remotes/origin/feature-payment remotes/origin/main
git branch -a shows all branches, including the remote-tracking ones (remotes/origin/…).
Team branching workflow
Hetvi's four rules for the Amroli Canteen team:
- Never commit directly to
main.mainmust always work. - One feature = one branch:
feature-cart,fix-total. - Pull before you start, so you branch from the latest
main. - Merge small and often. Small branches mean small conflicts.
$ git switch main$ git pull$ git switch -c feature-search$ git add .$ git commit -m "Add menu search box"$ git switch main$ git merge feature-search$ git branch -d feature-search
In Unit 5 you'll replace the local merge with a pull request on GitHub, so a teammate reviews the work first.
Unit 3 summary
- A branch is a movable label on a commit; HEAD marks the branch you are on.
git branchlists/creates;git switch(orcheckout) moves;-c/-bcreate and move.- Merge into the branch you are on:
git switch mainthengit merge feature-x. - Fast-forward just moves the label; a three-way merge creates a merge commit.
- A conflict = both sides changed the same lines. Edit, remove markers,
git add,git commit(orgit merge --abort). git branch -ddeletes merged branches;-Dforces;git push origin --deleteremoves it from GitHub.
Practise with Missions M-08 to M-11, then take the Unit 3 quiz.
Missions & labs
Practise in real Git: each mission runs in your browser and checks your work.