Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Work on Two Branches, Create a Merge Conflict on Purpose, Resolve It and Use git stash

Beginner35 minGit · Git Bash or Terminal · Any text editor

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 stash and brought back with git 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

  1. 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

  1. 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"
    
  2. 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"
    
  3. Merge blue into main first. This is a clean fast-forward:

    git switch main
    git merge feature/blue
    

    What you should see: Fast-forward and theme.txt | 2 +-

  4. Now merge green:

    git merge feature/green
    

    What 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

  1. Look at the file:

    cat theme.txt
    

    What you should see: HEAD is 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/green
    
  2. Decide 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 status
    

    What you should see: both modified: theme.txt

  3. Mark it resolved and finish the merge:

    git add theme.txt
    git commit -m "Merge feature/green: keep green button"
    git log --oneline --graph --all
    

    What you should see: a graph where two lines join into one merge commit.

  4. Delete the merged branches:

    git branch -d feature/blue feature/green
    

Part 4: Park unfinished work with stash

  1. Start some work you are not ready to commit:

    echo "font-size: 18px" >> theme.txt
    git status
    

    What you should see: modified: theme.txt

  2. The lead asks for an urgent fix. Park your edit:

    git stash push -m "font size work"
    git status
    git stash list
    

    What you should see: working tree clean and stash@{0}: On main: font size work

  3. Make the urgent fix and commit it:

    echo "# theme file" > README.md
    git add README.md
    git commit -m "Add README for theme"
    
  4. Bring your parked work back:

    git stash pop
    cat theme.txt
    

    What you should see: the font-size: 18px line 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

MistakeWhat happensFix
Committing the file with the <<<<<<< markers still insideBroken code goes to mainSearch for <<<<<<< before git add
Panicking and deleting the folderYou lose both branches' workUse git merge --abort to go back to before the merge
Using git stash and forgetting itWork seems lostRun git stash list; use git stash pop
Deleting a branch with -D before mergingUnmerged commits are thrown awayUse -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.