Labs · DevOps
Lab: Work on Two Branches, Create a Merge Conflict on Purpose, Resolve It and Use git stash
Course: DevOps · Chapter 3: Branch, Merge, Rebase, Stash and Pull Requests · Lesson: 3.15 Lab: colour, one conflict, one stash
Chapter 3 explains branch, merge, rebase and stash; this lab makes a real conflict on your own laptop.
Chala mitrano! Conflict mhanje bhiti nahi. Aaj aapan muddam conflict tayar karu, vachu ani solve karu.
Suppose we are…
Suppose we are two developers on an Instagram-style profile page team. Developer A changes the button colour to blue, developer B changes the same line to green. Both are right in their own branch. When both merge into main, Git cannot guess which colour wins, so it stops and asks a human. That is a merge conflict.
In this lab we play both developers on one laptop, create that exact conflict, fix it calmly, and then use git stash when the lead asks for an urgent fix in the middle of our work.
Goal of this lab
By the end you will have:
- Two feature branches merged into
main. - One merge conflict resolved by hand, with the markers removed.
- Unfinished work parked with
git stashand brought back withgit stash pop.
What you need
- Git installed, and the name and email set (lab 1 of this course).
- A terminal and a text editor (VS Code, Notepad++ or nano).
- 35 minutes.
Part 1: Prepare main
Create a fresh repo with one file:
mkdir colour-lab && cd colour-lab git init echo "button-colour: grey" > theme.txt git add theme.txt git commit -m "Add theme with grey button"
Part 2: Two branches change the same line
Developer A makes the button blue on a branch:
git switch -c feature/blue echo "button-colour: blue" > theme.txt git commit -am "Make button blue"Go back to main and let developer B make it green on another branch:
git switch main git switch -c feature/green echo "button-colour: green" > theme.txt git commit -am "Make button green"Merge blue into main first. This is a clean fast-forward:
git switch main git merge feature/blueWhat you should see:
Fast-forwardandtheme.txt | 2 +-Now merge green:
git merge feature/greenWhat you should see: a conflict, which is what we wanted:
Auto-merging theme.txt CONFLICT (content): Merge conflict in theme.txt Automatic merge failed; fix conflicts and then commit the result.
Part 3: Read and resolve the conflict
Look at the file:
cat theme.txtWhat you should see:
HEADis what is already in main (blue); the part after=======comes from the branch you are merging (green).<<<<<<< HEAD button-colour: blue ======= button-colour: green >>>>>>> feature/greenDecide with your team. Here we keep green. Edit the file so only the final line remains, with no markers:
echo "button-colour: green" > theme.txt git statusWhat you should see:
both modified: theme.txtMark it resolved and finish the merge:
git add theme.txt git commit -m "Merge feature/green: keep green button" git log --oneline --graph --allWhat you should see: a graph where two lines join into one merge commit.
Delete the merged branches:
git branch -d feature/blue feature/green
Part 4: Park unfinished work with stash
Start some work you are not ready to commit:
echo "font-size: 18px" >> theme.txt git statusWhat you should see:
modified: theme.txtThe lead asks for an urgent fix. Park your edit:
git stash push -m "font size work" git status git stash listWhat you should see:
working tree cleanandstash@{0}: On main: font size workMake the urgent fix and commit it:
echo "# theme file" > README.md git add README.md git commit -m "Add README for theme"Bring your parked work back:
git stash pop cat theme.txtWhat you should see: the
font-size: 18pxline is back and the stash list is empty.
Ravindra Bagale's Tip
Before you merge, run git pull on main. Most real conflicts happen because somebody merged from an old copy of main.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Committing the file with the <<<<<<< markers still inside | Broken code goes to main | Search for <<<<<<< before git add |
| Panicking and deleting the folder | You lose both branches' work | Use git merge --abort to go back to before the merge |
Using git stash and forgetting it | Work seems lost | Run git stash list; use git stash pop |
Deleting a branch with -D before merging | Unmerged commits are thrown away | Use -d; Git refuses if the branch is not merged |
Self-check checklist
0 of 4 done
Try-at-home challenge
Repeat Part 2, but this time run git rebase main on the green branch instead of merging. Resolve the conflict, run git rebase --continue, and compare the log graph with the merge version.
Samajla ka? Conflict is just Git asking you a question. Aata pudhe jaauya pull requests and code review.